Java program to check if a number is prime. Checking if a number is prime is a common programming exercise that helps you understand loops and conditional statements. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and itself.

### Java Program to Check if a Number is Prime

Let’s write a Java program to check if a number is prime. We will use a loop to check divisibility and determine if the number has any divisors other than 1 and itself.

Here’s the complete code:

```
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is prime
boolean isPrime = isPrime(number);
// Print the result
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
// Method to check if a number is prime
public static boolean isPrime(int n) {
// Edge cases: check if the number is less than 2
if (n <= 1) {
return false;
}
// Check for factors from 2 to the square root of n
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false; // n is divisible by i, so it's not a prime number
}
}
return true; // n is a prime number
}
}
```

### Explanation of the Code: (Java program to check if a number is prime)

Let’s break down the program step-by-step to understand how it works.

**Importing the Scanner Class**: We import the`Scanner`

class from the`java.util`

package to read input from the user.

```
import java.util.Scanner;
```

2. **Creating the Main Class**: We create a public class named `PrimeCheck`

. In Java, the class name should match the filename.

```
public class PrimeCheck {
```

3. **Main Method**: The `main`

method is the entry point of the program. Inside this method, we will write the code to check if a number is prime.

```
public static void main(String[] args) {
```

4. **Creating a Scanner Object**: We create a `Scanner`

object to read input from the user.

```
Scanner scanner = new Scanner(System.in);
```

5. **Reading Input from the User**: We prompt the user to enter a number and store this value in a variable.

```
System.out.print("Enter a number: ");
int number = scanner.nextInt();
```

6. **Checking if the Number is Prime**: We call the `isPrime`

method to check if the number is prime. The result is stored in a boolean variable `isPrime`

.

```
boolean isPrime = isPrime(number);
```

7. **Printing the Result**: We print whether the number is prime based on the value of `isPrime`

.

```
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
```

8. **Method to Check if a Number is Prime**: The `isPrime`

method checks if a number is prime. The method returns `true`

if the number is prime and `false`

otherwise.

```
public static boolean isPrime(int n) {
// Edge cases: check if the number is less than 2
if (n <= 1) {
return false;
}
// Check for factors from 2 to the square root of n
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false; // n is divisible by i, so it's not a prime number
}
}
return true; // n is a prime number
}
```

Java program to check if a number is prime

### Explanation of the `isPrime`

Method

**Edge Cases**: We check if the number is less than or equal to 1. If so, the number is not prime.

```
if (n <= 1) {
return false;
}
```

2. **Loop to Check Divisibility**: We use a `for`

loop to check if the number is divisible by any number from 2 to the square root of the number. If the number is divisible by any of these numbers, it is not prime.

```
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false; // n is divisible by i, so it's not a prime number
}
}
```

3. **Return True if No Divisors Found**: If the loop completes without finding any divisors, the number is prime, and we return `true`

.

```
return true; // n is a prime number
```

### Running the Program – Java program to check if a number is prime

To run the program, follow these steps:

- Save the code in a file named
`PrimeCheck.java`

. - Open a command prompt or terminal and navigate to the directory where you saved the file.
- Compile the program using the following command:

```
javac PrimeCheck.java
```

4. Run the compiled program using the following command:

```
java PrimeCheck
```

- Follow the prompt to enter a number. The program will display whether the number is prime or not.

Top 100 Java Programs: Click Here

Are you looking for a job: Click Here

### What did we learn from this article?

Checking if a number is prime is a fundamental exercise that helps in understanding loops and conditional statements in programming. This Java program demonstrates how to implement a prime check using a simple loop and mathematical logic. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills.