Java Program to Check Prime Number

Write a Java Program to Check Prime Number using For Loop, While Loop, and Functions. Prime Numbers is any natural number that is not divisible by any other number except 1 and itself.

Prime Numbers: 2, 3, 5,7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, etc

Java Program to Check Prime Number using For Loop

This Java program allows the user to enter any integer value. Next, this Java example checks whether the given number is a Prime number or Not using For Loop.

TIP: 2 is the only even prime number

// Java Program to Check Prime Number using For Loop
import java.util.Scanner;

public class PrimeNumber1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i, number, count = 0; 
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();		
		
		for (i = 2; i <= number/2; i++)
		{
			if(number % i == 0)
		    {
				count++;
		        break;
		    }	
		}
		if(count == 0 && number != 1 )
		{
			System.out.println( number + " is a Prime Number");
		}
		else
		{
		   System.out.println(number + " is Not a Prime Number");
		}
	}
}

Java Program to Check Prime Number using for loop output

Java Program to Check Prime Number 1

We initialized the integer i value to 2, and (i <= Number/2) condition to terminate when the condition fails.

Within the for loop, there is an If statement to check whether the Number divisible by i is exactly equal to 0 or not. If the condition is True, then Count incremented, and then Break Statement executed.

Next, we used another If statement to check whether Count is Zero, and Number is Not equal to 1. If it is True then prime Number

User entered integer in the above Java Program to Check Prime Number example is 11

First Iteration: for(i = 2; i <= 11/2; i++)
Condition (2 <= 5.5) is True. So, it checks the If Statement
if(number % i == 0) =>  if (11%2 == 0)  – Condition is False
i++ means i becomes 3

Second Iteration: for(i = 3; 3 <= 11/2; i++)
Condition (3 <= 5.5) is True.
if (11 % 3 == 0)  – Condition is False
i become 4

Third Iteration: for(i = 4; 4 <= 11/2; i++)
Condition (4 <= 5.5) is True.
if (11 % 4 == 0)  – Condition is False
i become 5

Fourth Iteration: for(i = 5; 5 <= 11/2; i++)
Condition (5 <= 5.5) is True.
if (11 % 5 == 0)  – Condition is False
i become 6

Fifth Iteration: for(i = 6; 6 <= 11/2; i++)

It means condition inside the For loop (6 <= 5.5) is False. So, the compiler comes out of the For Loop.

Next, it will enter into If statement. if(count == 0 && Number != 1 ). 

In all Five iterations, the Java If statement failed, so Count Value has not incremented from initialized 0, and Number = 11. So, the condition is True, and the number is Prime Number.

Java Program to Check Prime Number using While Loop

This program checks whether the given number is a Prime number or not using While Loop.

// Java Program to Check Prime Number using While Loop
import java.util.Scanner;

public class PrimeNumber2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i = 2, number, count = 0; 
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();		
		
		while(i <= number/2)
		{
			if(number % i == 0)
		    {
				count++;
		        break;
		    }
			i++;
		}
		if(count == 0 && number != 1 )
		{
			System.out.println( number + " is a Prime Number");
		}
		else
		{
		   System.out.println(number + " is Not a Prime Number");
		}
	}
}

We just replaced the For loop in the above Java example with the While loop. If you don’t know the While Loop, please refer: WHILE LOOP.

Java check prime number using a while loop output

 Please Enter any Number : 193
193 is a Prime Number

Let me enter another Value

 Please Enter any Number : 32
32 is Not a Prime Number

Java Program to Check Prime Number using Method

This Java program to find the prime number is the same as the first example. But we separated the logic and placed it in a separate method.

// Java Program to Check Prime Number using Method
import java.util.Scanner;

public class PrimeNumber3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, count = 0; 
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();
		
		count = PrimeNumber(number);
		
		if(count == 0 && number != 1 )
		{
			System.out.println( number + " is a Prime Number");
		}
		else
		{
		   System.out.println(number + " is Not a Prime Number");
		}
	}
	public static int PrimeNumber(int num)
	{
		int i, count = 0;
		
		for (i = 2; i <= num/2; i++)
		{
			if(num % i == 0)
		    {
				count++;
		        break;
		    }	
		}
		return count;
	}
}

Java Prime Number using functions Output

 Please Enter any Number : 103
103 is a Prime Number

Output 2

 Please Enter any Number : 242
242 is Not a Prime Number