Java Program To check a number is prime or not ?

By | 2018-03-21T21:57:50+00:00 June 21st, 2016|java programs|0 Comments

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

Check a Number is Prime or Not ?


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;
    }
}

Output :

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

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment