How to check whether a number is a prime number or not in java?

A Prime number is a number that is divisible by only 1 and itself. A prime number can’t be divided by zero, because numbers divided by zero are undefined. The smallest prime number is 2, which is also the only even prime.

Let’s see the logic to check whether a number is a prime number or not,

Solution

```package com.javacodepoint.programs;

import java.util.Scanner;

public static void main(String[] args) {

System.out.print("Enter an integer number:");

// Reading user input using Scanner object
int number = new Scanner(System.in).nextInt();

if(number > 1 && checkPrime(number)) {
System.out.println("The given number is a prime number.");
}else {
System.out.println("The given number is not a prime number.");
}

}

/*
* Method to check whether number is prime or not
*/

public static boolean checkPrime(int number) {

boolean isPrime = true;

for(int i=2; i <= (number/2); i++ ) {

if(number % i == 0) {
isPrime = false;
break;
}

}

return isPrime;

}
}
```

OUTPUT:

Enter an integer number:13
The given number is a prime number.