Tuesday, December 13, 2011

Prime Number Program In Java

import java.util.*;
import java.io.*;
class prime
{
public static void main(String args[])throws Exception
{
int c=0,i;
System.out.println("enter a number");
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(k.readLine());
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println(n+" is prime");
else
System.out.println(n+" is not prime");
}
}


Output:
enter a number:
7
7 is prime.