Write a Java program to check if the number entered by the user is Prime number or not.

# Write a Java program to check if the number entered by the user is Prime number or not.

Image is Unavailable


Algorithm

  1. Start
  2. x:=0
  3. If x<=1, go to step 4 or else go to step 8
  4. Print "Enter a number"
  5. Read x
  6. If x<=1, go to step 7 or else go to step 8
  7. Print "Invalid Input. Try Again". 
  8. count:=0
  9. i:=2
  10. If i<x, go to step 11 or else go to step 14
  11. If x%i==0, go to step 12 or else go to step 13
  12. count ++
  13. i++. Go to step 10.
  14. If count>0, go to step 15 or else go to step 16
  15. Print "The given number is not a prime number". Go to step 17
  16. Print "The given number is a prime number" 
  17. Stop

Program

Buffered Reader

import java.io.*;
class Prime
{
    int x;
    void accept() throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(x<=1)
        {
        System.out.println("Enter a number");
        x=Integer.parseInt(br.readLine());
        if(x<=1)
        {
            System.out.println("Invalid Input. Try Again.");
        }
    }
    }
    boolean checkPrime()
    {
        int count = 0;
        for(int i=2;i<x;i++)
        {
            if(x%i==0)
            {
                count++;
            }
        }
        if (count > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    void displayt()
    {
        System.out.println("The given number is a prime number");
    }
    void displayf()
    {
        System.out.println("The given number is not a prime number");
    }
    void main() throws IOException
    {
        accept();
        if(checkPrime()==true)
        {
            displayt();
        }
        else
        {
            displayf();
        }
    }
}

Scanner

import java.util.*;
class Prime
{
    int x;
    void accept() 
    {
        Scanner sc=new Scanner(System.in);
        while(x<=1)
        {
        System.out.println("Enter a number");
        x=sc.nextInt();
        if(x<=1)
        {
            System.out.println("Invalid Input. Try Again.");
        }
    }
    }
    boolean checkPrime()
    {
        int count = 0;
        for(int i=2;i<x;i++)
        {
            if(x%i==0)
            {
                count++;
            }
        }
        if (count > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    void displayt()
    {
        System.out.println("The given number is a prime number");
    }
    void displayf()
    {
        System.out.println("The given number is not a prime number");
    }
    void main() 
    {
        accept();
        if(checkPrime()==true)
        {
            displayt();
        }
        else
        {
            displayf();
        }
    }
}

Documentation

Variable Description

 Data Type Variable NameDescription 
 int
 Accept a value from the user/
 int i Acts as a counter variable
 int count Counts the number of factors of a number except 1 and itself.

Method Description

 Return TypeFunction NameDescription 
 voidaccept() Accepts a value from the user 
boolean checkPrime() Checks if the given number is prime or not. 
 voiddisplayt() Displays the message if the condition is true 
void displayf() Displays the message if the condition is false 
void main() Acts as an execution gateway to the program 

Output:

Image Unavailable
Image Unavailable
Image Unavailable

Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @IscJavaCode
Twitter: @IscCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful


Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences