Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Write a program to check whether number is prime number

  1. /*
  2.  * Write a program to check whether number is prime number. Number should
  3.  * be read from the console.
  4.  */
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9.  
  10. public class CheckPrimeNumber {
  11.  
  12.  public static void main(String args[]){
  13.  
  14.   System.out.println("Enter Number");
  15.  
  16.   try{
  17.    
  18.    //read the number from console
  19.    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  20.    int number = Integer.parseInt(br.readLine());
  21.    
  22.    boolean isPrime = true;
  23.  
  24.    for(int i = 2; i < number; i++){
  25.     if(number % i ==0){
  26.      isPrime = false;
  27.      break;
  28.     }
  29.    }
  30.    
  31.    if(isPrime)
  32.     System.out.print("Number is prime number.");
  33.    else
  34.     System.out.println("Number is not prime number.");
  35.  
  36.   }catch(NumberFormatException nef){
  37.    System.out.println("Invalid number.");
  38.   }catch(IOException ioe){
  39.    System.out.println("IO error" + ioe);
  40.   }
  41.  
  42.  }
  43.  
  44. }
Your rating: None Average: 3 (6 votes)
Share this