# 1. Introduction

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A simple Java program can check whether a given number is prime through iterative division. This is a fundamental exercise for learning the concepts of loops and conditional statements in Java.

# 2. Program Steps

1. Import the *Scanner* class from the *java.util* package for user input.

2. Define the main class named *PrimeNumberChecker*.

3. Inside the main class, define the *main* method.

4. Inside the *main* method, create an object of the *Scanner* class to take user input.

5. Prompt the user to enter a number and store it in a variable.

6. Initialize a boolean variable *isPrime* to *true*.

7. Use a *for* loop to check divisibility from 2 to the square root of the number.

8. If the number is divisible by any number in the range, set *isPrime* to *false* and break the loop.

9. Print whether the number is prime or not based on the value of *isPrime*.

# 3. Code Program

```
import java.util.Scanner; // 1. Importing Scanner class for user input
public class PrimeNumberChecker { // 2. Defining the main class
public static void main(String[] args) { // 3. Defining the main method
Scanner input = new Scanner(System.in); // 4. Creating Scanner object to take user input
System.out.print("Enter a number to check if it is prime: "); // 5. Prompting user for a number
int num = input.nextInt(); // Storing the number in variable num
boolean isPrime = true; // 6. Initializing isPrime to true
// 7. Checking divisibility from 2 to the square root of num
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) { // 8. If num is divisible by i, then it is not prime
isPrime = false; // Setting isPrime to false
break; // Exiting the loop
}
}
// 9. Printing whether the number is prime or not
if (isPrime && num > 1) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
input.close(); // Closing the Scanner object to avoid memory leak
}
}
```

### Output:

Enter a number to check if it is prime: 29 29 is a prime number.

# 4. Step By Step Explanation

– Step 1: The *Scanner* class from *java.util* package is imported to facilitate user input.

– Step 2: The main class *PrimeNumberChecker* is defined.

– Step 3: The *main* method is defined within the main class to act as the entry point of the program.

– Step 4: A *Scanner* object named "input" is created to read user input.

– Step 5: The user is prompted to input a number, *29*, which is stored in the variable *num*.

– Step 6: A boolean variable *isPrime* is initialized to *true*.

– Step 7: A *for* loop iterates from *2* to the square root of *num*, checking divisibility.

– Step 8: If *num* is divisible by any number in the range, *isPrime* is set to *false* and the loop is exited.

– Step 9: Based on the value of *isPrime*, the program prints out whether the number *29* is prime or not.

– Finally, the *Scanner* object is closed to avoid resource leaks.