Here we are going to write a program to check a number is prime or not ?

Check a Number is Prime or Not ?

[java]

import java.util.Scanner;

public class CheckPrimeNumber {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any Binary Number : ");
        int input = sc.nextInt();
        System.out.println("Is "+input+" Prime .. ? "+checkPrimeNumber(input));
    }
    
    public static boolean checkPrimeNumber(int number) {
        boolean isPrime = false;

        if (number > 0) {
            int counter = (int) Math.sqrt(number);
            int incrementer = 0;
            while (counter > 1) {
                if (number % counter == 0) {
                    ++incrementer;
                }
                –counter;
            }
            if (incrementer == 0) {
                isPrime = true;
            }
        }
        return isPrime;
    }
}

[/java]

Output :

Enter any Binary Number : 7
Is 7 Prime .. ? true
Enter any Binary Number : 10
Is 10 Prime .. ? false

Happy Learning 🙂