Saturday, 1 September 2012

Prime Numbers java program


Prime Numbers
Output
prime numbers are:
2
3
5
7
11

 Source code
Prime.java

package com.swain.cell;

public class Prime {
      
        public static void main(String args[])
          {
             int n, status = 1, num = 3;
               n=5;
             if ( n >= 1 )
             {
                System.out.println("prime numbers are: ");
                System.out.println(2);
             }
        
             for ( int count = 2 ; count <=n ;  )
             {
                for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
                {
                   if ( num%j == 0 )
                   {
                      status = 0;
                      break;
                   }
                }
                if ( status != 0 )
                {
                   System.out.println(num);
                   count++;
                }
                status = 1;
                num++;
             }        
          }

}



No comments:

Post a Comment