Java

Program to check whether input number is prime or not using while loop.

Program to check whether input number is prime or not using while loop.
import java.util.Scanner;
class demo1
{
   public static void main(String args[])
   {		
	int temp;
	boolean PrimeNumber=true;
	Scanner scan= new Scanner(System.in);
	System.out.println("Enter any number:");
	int num=scan.nextInt();
        scan.close();
	int i=2;
        while(i<= num/2)
         {
           if(num % i == 0)
             {
               	PrimeNumber = false;
	           break;
             } 
                  i++;
          }
		if(PrimeNumber)
	   System.out.println(num + " is a Prime Number");
	else
	   System.out.println(num + " is not a Prime Number");
   }
}
Nikhil Juneja

Passionate software developer and technical contributor at TutorialsLink.