Frequently Asked Java Program 11: Check Given Number is Prime Or Not
Table of Contents
Introduction
In this tutorial, we will learn how to check if a given number is a prime number using Java. A prime number is a natural number greater than one that has no positive divisors other than 1 and itself. Understanding how to determine if a number is prime is fundamental in number theory and has practical applications in fields such as cryptography and computer science.
Step 1: Understand Prime Numbers
Before we write the code, let’s clarify what prime numbers are:
- A prime number is greater than 1.
- It has exactly two factors: 1 and the number itself.
- Examples of prime numbers include 2, 3, 5, 7, 11, and 13.
- Numbers like 4, 6, 8, and 28 are not prime because they have more than two factors.
Step 2: Set Up Your Java Environment
To write and execute Java code, ensure you have the following installed:
- Java Development Kit (JDK)
- An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VSCode.
Step 3: Write the Java Code to Check for Prime Numbers
Here is a simple Java program that checks whether a number is prime:
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
public static boolean isPrime(int num) {
if (num <= 1) return false; // Not prime if less than or equal to 1
for (int i = 2; i <= Math.sqrt(num); i++) { // Check up to the square root of num
if (num % i == 0) {
return false; // Not prime if divisible by any number
}
}
return true; // Prime if no divisors found
}
}
Code Explanation
- The program starts by importing the Scanner class for user input.
- It prompts the user to enter a number.
- The
isPrime
method checks if the number is prime:- It returns false if the number is less than or equal to 1.
- It loops from 2 to the square root of the number, checking for divisibility.
- If the number is divisible by any of these, it is not prime.
Step 4: Compile and Run Your Program
To compile and run the program:
- Save the code in a file named
PrimeCheck.java
. - Open a terminal or command prompt.
- Navigate to the directory where the file is saved.
- Compile the code using:
javac PrimeCheck.java
- Run the program using:
java PrimeCheck
Step 5: Test with Different Numbers
After running your program, test it with various inputs:
- Input 19 and check the output (should be prime).
- Input 28 and check the output (should not be prime).
- Explore edge cases like 0, 1, and negative numbers to see how the program responds.
Conclusion
In this tutorial, we learned how to determine if a number is prime using Java. We covered the definition of prime numbers, set up a Java environment, and wrote a program to check for primality. You can expand this program by adding features such as error handling or checking for multiple numbers in a single execution. Experiment with the code and enhance your programming skills!