Select Page

Prime Number Program in Java using for loop

A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In other words, a number which is divisible by itself and 1 is called the prime number. For example, 2, 3, 5, 7, 11, 17, 97, 109, 139, 173, 199, 1151, 1289, 1367, etc. Remember that 2 is an even number which is also a prime number.

In this section, we will create a prime number program in Java by using different logics. Also, create a Java program to find the prime number between the two given numbers.

Let’s write a Java program to check the given number is prime or not.

Output:

Explanation: In the above program, we are trying to find a factor, excepting 1 and the number itself, of the given number. If we are unable to get any factor, the given number is a prime number; otherwise, it is not a prime number.

Let’s create an other Java program to check the number is prime or not.

Output:

### Find Prime Numbers Between Two Given Numbers

We can even find prime numbers between the two given numbers. The demonstration is given below.