A number is said to be prime if it is divisible by 1 and itself. otherwise it is composite.
Step 1: START.
Step 2: read the number n.
Step 3: Check the divisibility of the number from 2 to n/2.
Step 4: If number is divisible by any of the numbers above . It isn’t prime.
Step 5 :Else it is prime.
Step 6: STOP.
public static void main(String args)
System.out.println("\n \n *** www.programmingposts.com *** \n \n");
System.out.println("<< JAVA Program to find the given number is prime or not >> \n \n");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
int num = scanner.nextInt();
int m = num/2;
if(num%i == 0)
System.out.println(num + " is a Composite number");
System.out.println(num + " is a Prime number");
If we want to check whether the given number (num) is a prime or not then divide the number by 2 and then you will get a value then divide the number (num) with all the numbers less than num/2 if you are not getting zero then it is a prime number
now divide the number 23 with 2…..upto 11 . If you get zero anywhere then it is not a prime number
But here we won’t get a zero since 23 is a prime number
*** www.programmingposts.com ***
<< JAVA Program to find the given number is prime or not >>
Enter the number:
11 is a Prime number
Screenshot of Output: