Java Program to Check the Number is Prime or Not

Java Program to Check the Number is Prime or Not

Write a program Java to check if the number input by the users is prime or not.
import java.util.*;
class ranjan1
 {
   public static void main(String s[])
   {
     int count=0,n,i;
	 Scanner sc=new Scanner(System.in);
     System.out.println("Enter a number:");
	 n=sc.nextInt();	
     for(i=1;i<=n;i++)
     {
       if(n%i==0)
       count++;   
     }
     if(count==2)
       System.out.print(n+" is a prime number");
     else
       System.out.print(n+" is not a prime number");
   }
 }

Output:1

Enter a number:
23
23 is a prime number

Output:2

Enter a number:
4
4 is not a prime number

More related java program posts:

Post a Comment

0 Comments