A prime number is a positive integer which is divisible by itself and only 1
Steps to check prime number
Accept the number from user ans store it into the n variable
Start the loop from 2 and check number n is divisible by loop variable or not.
if by any of the number the n is divisible, then update flag value to 1 and stop the loop.
check if flag = 1 then display number is not prime.
check if flag = 0 then display number is prime.
See the following flowchart to clearly understand the working of loop to find out number is prime or not.
Example - Program to check given number is prime or not
public class PrimeNumberEx
public static void main(String args)
Scanner scan = new Scanner(System.in);
i = 2; flag = 0;
System.out.print("Enter any number : ");
n = scan.nextInt();
if(n%i == 0)
flag = 1;
i = i + 1; // or i += 1;
System.out.print(n+" is a prime number");
System.out.print(n+" is Not prime number");