Any natural number that is not divisible by any other number except 1, and itself is called as Prime Number. In this article, we will show you, How to write a Java Program to Check Prime Number using For Loop, While Loop, and Functions.

**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

**TIP:** 2 is the only even prime number

## Java Program to Check Prime Number using For Loop

This program allows the user to enter any integer value. Next, it will check whether the given number is a Prime number or Not using For Loop.

// 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"); } } }

**OUTPUT**

**ANALYSIS**

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 If statement to check, whether the Number divisible by i is exactly equal to 0 or not. If the condition is True then Count will be incremented, and then Break Statement will be 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 example is 11

**First Iteration: **for(i = 2; i <= 11/2; i++)

Condition (2 <= 5.5) is True. So, it will check the If Statement

if(number % i == 0) => if (11%2 == 0) – Condition is False

i++ means i will become 3

**Second Iteration: **for(i = 3; 3 <= 11/2; i++)

Condition (3 <= 5.5) is True. So, it will check the If Statement

if (11 % 3 == 0) – Condition is False

i will become 4

**Third Iteration: **for(i = 4; 4 <= 11/2; i++)

Condition (4 <= 5.5) is True. So, it will check the If Statement

if (11 % 4 == 0) – Condition is False

i will become 5

**Fourth Iteration: **for(i = 5; 5 <= 11/2; i++)

Condition (5 <= 5.5) is True. So, it will check the If Statement

if (11 % 5 == 0) – Condition is False

i will become 6

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

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

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

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

## Java Program to Check Prime Number using While Loop

This program will check whether the given number is 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 example with the While loop. If you don’t understand the While Loop, please refer: WHILE LOOP

Let me enter another Value

## Java Program to Check Prime Number using Method

This program is same as first example but we separated the logic and placed it in the 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; } }

**OUTPUT**

Thank you for Visiting Our Blog

## Leave a Reply