Java program to find prime number

Thursday, 19 January 2012


Here is the program to find prime number between 1 to given number.

 import java.io.*;

class PrimeNumber {
  public static void main(String[] args) throws Exception{
  int i;
  BufferedReader bf = new BufferedReader(
  new InputStreamReader(System.in));
  System.out.println("Enter number:");
  int num = Integer.parseInt(bf.readLine());
  System.out.println("Prime number: ");
  for (i=1; i < num; i++ ){
  int j;
  for (j=2; j<i; j++){
  int n = i%j;
  if (n==0){
  break;
  }
  }
  if(i == j){
  System.out.print("  "+i);
  }
  }
  }
}

OUTPUT : 
Enter number:
25
Prime number:
  2  3  5  7  11  13  17  19  23

0 comments:

Post a Comment