On this tutorial page we are going to learn how to write a Java program to check whether the number is prime or not.

A prime is a natural number that is greater than 1 and divided by 1 or itself only. If any number is divisible by any other number, then that number is not a prime number.

Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23 ...

Let's see the Java program implementation.

Program 1: Java Program to Check Prime Number using For Loop

Output

7 is a prime number. 16 is not a prime number. 25 is not a prime number. 29 is a prime number.

Program 2: Java Program to Check Prime Number using While Loop

Output 1:

Enter the number: 11 11 is a prime number.

Output 2:

Enter the number: 15 15 is not a prime number.

That's all guys, hope this Java Program is helpful for you.

Happy Learning... 😀

Please share this article on social media to help others.

If you have any query or suggestion regarding this artical please share with us feedback@javabytechie.com