Java Program To Find Whether A Number Is Prime Or Not

Java Program To Find Whether A Number Is Prime Or Not

Java Program To Find Whether A Number Is Prime Or Not

import java.unit.Scanner;

public class Prime  {

     public static void main(String[ ] args)  {
             
           int temp, num;
           boolean isPrime = true;
           Scanner in = new Scanner(System.in);
           num = in.nextInt( );
           in.close( );
           for (int i = 2; i<= num/2; i++)  {
                  temp = num%i;
                  if  (temp == 0)  {
                         isPrime = false;
                         break;
                  }
        }
         if(isPrime)
               System.out.println(num + "number is prime" );
               else
                      System.out.println(num + "number is not a prime" );


      }


}

Output:


445
445number is not a prime

  •  The Above program will give you a clear cut idea on how to find the given number is prime or not

0 comments 10:

Post a Comment