July 05, 2013

Java Program to print Prime numbers

Write an Java Program to print Prime numbers



PRIME

import java.io.*;
class prime
{
            public static void main(String[] args)throws IOException
                        {
                        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));                       
                        System.out.println("Enter a no:");
                        int num=Integer.parseInt(stdin.readLine());           
                        int i;
                           for(i=2; i < num ;i++ )
                                       {
                                          int rem = num%i;
                                                      if (rem==0)
                                                                  {
                                                                    System.out.println(num+ " is not a Prime number");
                                                                    break;
                                                                 }
                                      }
                                        if(i == num)
                                       {
                                          System.out.println(num+ " is a Prime number");
                                        }                                                 
                        }
}
  
OUTPUT

C:\Program Files\Java\jdk1.5.0\bin>java prime
Enter a no:
3
3 is a Prime number

No comments:
Write comments