A natural number that is greater than 1 , divided by 1 and itself is called prime number. In this tutorial, we are going to write a program to identify given number is prime or not.


import java.util.Scanner;
public class PrimeNumber {
 
    public boolean isPrimeNumber(int number){
	    if (number == 0 || number==1)
		{
			return false ;
		}
		if (number == 2)
		{
			return true ;
		}
        for(int i=2; i<=number/i; i++){
            if(number % i == 0){
                return false;
            }
        }
        return true;
    }
    public static void main(String args[]){
           PrimeNumber primenumber = new PrimeNumber();
		   System.out.print("Please Enter Number: ");
           Scanner read = new Scanner(System.in);
           int number = read.nextInt();
		   System.out.println("Is "+number+" prime number : " +primenumber.isPrimeNumber(number));
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *