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

Related Examples