Java Program to Check Prime Number or Not

Java Program to Check Prime Number or Not. In the java programming language, you can choose whether to program in prime or not prime mode. For new learn beginners in the java programming language, this tutorial will show you how to verify if a number is a prime number or not in a java program. A single-digit number and 1 (e.g. 2, 3, 5, 7, 11)

Flag and Boolean Function in Java

In programming, the flag variable is used as a signal to inform the computer that a specific condition has been satisfied. It’s generally used as a boolean variable, showing whether a condition is true or false.

Boolean Function in Java

The Java Boolean class’s equals() function returns a Boolean result. If the argument is not null and is a Boolean object with the same Boolean value as this object, it returns true; otherwise, it returns false.

Prime Number Definition

A prime number is a number higher than one that has just two factors: itself and one. A prime number cannot be divided by another prime number without a remainder. The number 13 is an example of a prime number. 

To determine whether a number is a prime number, divide it by two and see if the result is a whole number. It can’t be a prime number if you do. If you still don’t have a full number, divide it by prime numbers: 3, 5, 7, 11, and so on, always dividing by a prime number and so on. 

Now, increment N by 1 in each iteration until that variable does not equally true, and check whether it is prime or not. If it’s prime, print it and set the discovered variable’s value to True. Otherwise, repeat the loop until the next prime number is found.

Java Program to Check Prime Number or Not

public class Main 
public static void main(String[] args) 
int num = 100;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) 
if (num % i == 0) {
flag = true;
if (!flag)
System.out.println(num + " is a prime number");
System.out.println(num + " is not a prime number");


100 is not a prime number