Java Program to Find if a Number is Prime or Not

Java program for prime numbers

In this post, we have demonstrated how you can check whether a number is prime or not. This can be done using a for loop and while loop in Java.

A number which is divisible by 1 and itself is a prime number. Therefore, if any number is divisible by any other number, it is not a prime number.

The following program takes the number and then checks whether the input number is prime or not. The program then displays the result.

Example:

How to Check Prime Number using a for loop

After you run the program, the output will be:

Example 2:

How to Check Prime Number using a while loop

When you run the program, the output will be:

Hope you found this post useful. For more updates on java program for prime numbers, keep visiting the blog. Thanks for reading!

print

Leave a Reply

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