recent

Java program print prime numbers

Java program print prime numbers
/* java program prints prime numbers, number of prime numbers required is asked from the user.smallest prime number is 2*/
import java.util.*;

class PrimeNum
{
   public static void main(String args[])
   {
      int no,j, status = 1, num = 3;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter no. of prime numbers to print");
      n = in.nextInt();

      if (no >= 1)
      {
         System.out.println("First "+no+" prime numbers :-");
         System.out.println(2);
      }

      for ( int count = 2 ; count <=no ;  )
      {
         for ( 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++;
      }      
   }
}
Powered by Blogger.