This post is all about How write a **java program to check whether a number is prime or not **We will write a java program to whether a input number is prime or not.

As we know that a number is a prime number if it is a natural number greater than 1 and it can be divided either by 1 or by the number itself. As example- 2, 3, 5, 7, 11, 13, 17,19,23..….

When we looked into definition of prime number first thing that may come to your mind to write java program for checking prime number is to have a variable in a for loop that starts from 2 (as 1 will always divide the number) and increment it by one until it reaches the number that we have to check for being prime number or not. In every iteration of the loop divide the input number by variable, if remainder is zero at any point of time then the checked number is not a prime number.

And logic would look something like below–

1 2 3 4 5 6 7 8 |
int number = 25; boolean flag = true; for(int i = 2; i < number; i++){ if(number % i == 0){ flag = false; break; } } |

But this logic can be made more efficient. if We check a number is prime or not you need to run a loop starting from 2 till number/2 to check if number has any divisor exits or not.

**As example –** If number is 10 then you just need to check till 5 (10/2) to see if it divides by any number or not. Same way if you have a number 25 you just need to check till 12(25/2) to see if it divides completely by any number or not. We’ll use the same concept/logic to write our java program to check for prime number.

#### Note: You should note that 0 and 1 are not prime numbers. The Number two(2) is the only even prime number because all the other even numbers can be divided by 2.

Let’s write a java program check for prime number:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.kkjavatutorials.clients; import java.util.Scanner; /** * * @author KK JavaTutorials *THis java Java program is all about How to check whether a number is prime or not */ public class CheckPrimeNumberTest { public static void main(String[] args) { Scanner scanner = null; try { //Taking input from the user using scanner scanner = new Scanner(System.in); System.out.println("Enter number which u want to check whether prime or not:"); int number = scanner.nextInt(); boolean flag = isPrimeNumber(number); if(flag){ System.out.println(number + " is a prime number."); }else{ System.out.println(number + " is not a prime number."); } } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null) scanner.close(); } } private static boolean isPrimeNumber(int number){ boolean flag = true; if(number <= 1){ flag = false; return flag; } //for loop start from 2 and increment it till number/2 for(int i = 2; i <=number/2; i++){ //if no remainder found means we get divider if(number % i == 0){ flag = false; break; } } return flag; }} |

**Now let’s run above program to test few inputs:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter number which u want to check whether prime or not: 12 12 is not a prime number. Enter number which u want to check whether prime or not: 19 19 is a prime number. Enter number which u want to check whether prime or not: 80 80 is not a prime number. Enter number which u want to check whether prime or not: 1 1 is not a prime number |

That’s all about this post **Java program to check whether a number is prime or not**

**You May Also Like:**

How to swap two numbers with or without temporary variable in java

Java Program to Swap two numbers using Bitwise XOR Operator?

How to reverse a number in Java

How to check Armstrong number java program

Java program to find factorial of a number

Java Program to Calculate the Power of a Number

Java Program to displaying prime numbers

Fibonacci series using iterative and recursive approach java program

Find largest and second largest number in the given Array

Java program to Remove Duplicate Elements From an Array

Find common elements between two Arrays

Find largest and smallest number in the given Array

Java Program to find duplicate elements in an Array

Count number of words in a string in java

How would you check if a number is even or odd using bit wise operator in Java?

How can you check if the given number is power of 2?

How to check if String is number in java ?

**If you have any feedback or suggestion please feel free to drop in blow comment box.**