Java Programming

Prime Number

Java programming example for checking the given number is prime or composite

9/3/2021
0 views
prime-number.javaJava
import java.io.*;

class compute {
	boolean calculate(int data) {

		/* Loop the process upto the half of the number */
		for(int i = 2; i <= data / 2; i++)
			if(data % i == 0)
				return false;
		return true;
	}
}
										
class prime {
	public static void main(String args[ ]) {
		compute cp = new compute();
		if(cp.calculate(Integer.parseInt(args[0])) == true)
			System.out.println("It's a Prime Number");
		else
			System.out.println("It's a Composite Number");
	}
}



/* Output */
java prime 4
It's a Composite Number

/* ------------------------- */

java even_odd 7
It's a Prime Number
Prime NumberComposite NumberJava tutorial

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com