Program to check whether the number is prime or not in java

Program to check whether the number is prime or not in java. 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)

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;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number");
else
System.out.println(num + " is not a prime number");
}
}

 

Output:

100 is not a prime number

What is 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.

What is boolean function in java to print the prime number

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.

What is prime number and how to find the prime easily in java

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.