coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

Java Program To Check If The Given Number is Prime or Not

In this tutorial, we will see a Java program to check if the given number is prime or not.

What is a Prime Number?

A number that can be divided exactly only by itself and 1.

For example 7, 17 and 41.

Java Program To Check If The Given Number is Binary Or Not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class PrimeNumber {

    public static void main(String[] args) {
        int num = 41;
        boolean isPrimeNumber = isPrime(num);
        if (isPrimeNumber) {
            System.out.println(num + " is a prime number.");
        } else {
            System.out.println(num + " is not a prime number.");
        }
    }

    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Output

1
41 is a prime number.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus