Find Out The Given Number Is Prime Or Not by using Java Program

Code:

import java.util.*;
import java.lang.Math;

public class PrimeNumber
{
    public static void main(String[] args)
    {
         int n,i,c=1;

         Scanner sc = new Scanner(System.in);

         System.out.print("\nEnter The Number : ");
         n = sc.nextInt();

         for(i=2;i<=Math.sqrt(n);i++)
         {
              if(n%i==0)
              {
                   c=0;
              }
        }
if(c==1)
{
               System.out.println("\n"+n+" Is A Prime Number\n");
        }
        else
        {
               System.out.println("\n"+n+" Is Not A Prime Number\n");
        }
    }
}

Output of program:


No comments

Powered by Blogger.