Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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");
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now