Prime Number Program in Java
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.
Filename: PrimeNumberExample.java
public class PrimeNumberExample { public static void main(String[] args) { int n = 5; // The number stored in ‘n’ will be checked for prime int countFactors = 0; // count factors of the number ‘n’ if(n <= 1 ) // A prime number can never become less than 1 System.out.println(n + " is not the prime number"); else { for(int i = 2; i <= Math.sqrt(n); i++) { if(n%i == 0) countFactors++; if(countFactors > 0) { System.out.println(n + " is not the prime number"); break; // breaks the loop if no factor is found } } if(countFactors == 0) System.out.println(n + " is the prime number"); } } }
Output:
5 is the prime number
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.
Filename: PrimeNumberExample1.java
public class PrimeNumberExample1 { static void isPrime(int n) { int countFactors = 0; if(n <= 1) { System.out.println(n + " is not the prime number"); return; } for(int i = 2; i <= Math.sqrt(n); i++) { if(n%i == 0) //increments the countFactors by 1 countFactors++; if(countFactors > 0) { System.out.println(n + " is not the prime number"); break; // A factor is found, no need to check further. } } if(countFactors == 0) System.out.println(n + " is the prime number"); } public static void main(String[] args) { isPrime(2); isPrime(3); isPrime(10); isPrime(17); } }
Output:
2 is the prime number 3 is the prime number 10 is not the prime number 17 is the prime number
Find Prime Numbers Between Two Given Numbers
We can even find prime numbers between the two given numbers. The demonstration is given below.
Filename: PrimeNumberExample2.java
public class PrimeNumberExample2 { static boolean isPrime(int n) { int countFactors = 0; if(n <= 1) return false; for(int i = 2; i <= Math.sqrt(n); i++) { if(n%i == 0) countFactors++; if(countFactors > 0) return false; } return true; } public static void main(String[] args) { int firstNumber = 1, secondNumber = 20; for(int i = firstNumber; i <= secondNumber; i++) { if(isPrime(i)) System.out.println(i + " is the prime number"); } } }
Output:
2 is the prime number 3 is the prime number 5 is the prime number 7 is the prime number 11 is the prime number 13 is the prime number 17 is the prime number 19 is the prime number