A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
import java.util.*;
class primeno
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,c=0;
System.out.println("Enter the no.");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
Output:
Enter the no:
11
Prime
Enter the no:
10
Not Prime
import java.util.*;
class primeno
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,c=0;
System.out.println("Enter the no.");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
Output:
Enter the no:
11
Prime
Enter the no:
10
Not Prime