Thursday , May 25 2017
Home / java programs / Java Program To check a number is prime or not ?

Java Program To check a number is prime or not ?

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

About chandrashekhar

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+ ,

Check Also

Java Program For Binary Addition

Here we are going to write a Java program for Binary Addition. As part of …

Leave a Reply

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