Computer Science, asked by samriddhiray, 3 months ago

WAP to accept a number (through BufferedReader and try catch block) and find whether it is prime or not.
//can someone do the program for me//​

Answers

Answered by dreamrob
0

Program:

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class MyClass

{

   public static void main(String args[])

   {

       BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

       System.out.print("Enter a number : ");

       int n = 0;

       try

       {

           n = Integer.parseInt(bf.readLine());

       }

       catch (Exception e)

       {

           e.printStackTrace();    

       }

       int count = 0;

       for(int i = 1 ; i <= n ; i++)

       {

           if((n%i) == 0)

           {

               count++;  

           }

       }

       if(count == 2)

       {

           System.out.print("It is a Prime Number");  

       }

       else

       {

           System.out.print("It is not a Prime Number");  

       }

   }

}

Similar questions