Java Program to Check Prime Number
PROGRAMMING
3/18/20243 min read
Java Program to Check Prime Number
In Java, a prime number is a number greater than 1 that has no positive divisors other than 1 and itself. We can write a program to check whether a given number is prime or not using the following code:
public class PrimeNumberChecker {
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int number = 29;
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
Explanation of the Syntax
The code above uses a method called isPrime to check whether a given number is prime or not. Here's a breakdown of the syntax:
public class PrimeNumberChecker: This line declares a public class namedPrimeNumberChecker.public static boolean isPrime(int number): This line declares a public static method namedisPrimethat takes an integer parameter namednumberand returns a boolean value.if (number <= 1): This line checks if the given number is less than or equal to 1. If it is, the method returnsfalseas prime numbers are greater than 1.for (int i = 2; i <= Math.sqrt(number); i++): This line initializes a for loop that starts from 2 and iterates up to the square root of the given number. It checks if the number is divisible by any number between 2 and its square root. If it is, the method returnsfalseas it is not a prime number.public static void main(String[] args): This line declares the main method, which serves as the entry point of the program.int number = 29: This line declares an integer variable namednumberand assigns it the value 29. You can change this value to test different numbers.if (isPrime(number)): This line checks if the given number is prime by calling theisPrimemethod. If it returnstrue, it prints that the number is prime. Otherwise, it prints that the number is not prime.
Alternative Java Code
Another way to check whether a number is prime is by using the BigInteger class, which provides methods for performing arithmetic operations on large integers. Here's an alternative code snippet:
import java.math.BigInteger;
public class PrimeNumberChecker {
public static boolean isPrime(BigInteger number) {
if (number.compareTo(BigInteger.ONE) <= 0) {
return false;
}
for (BigInteger i = BigInteger.valueOf(2); i.compareTo(number.sqrt()) <= 0; i = i.add(BigInteger.ONE)) {
if (number.mod(i).equals(BigInteger.ZERO)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
BigInteger number = new BigInteger("29");
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
Explanation of the Alternative Code
This alternative code uses the BigInteger class to handle large integer values. Here's a breakdown of the syntax:
import java.math.BigInteger: This line imports theBigIntegerclass from thejava.mathpackage.public static boolean isPrime(BigInteger number): This line declares a public static method namedisPrimethat takes aBigIntegerparameter namednumberand returns a boolean value.if (number.compareTo(BigInteger.ONE) <= 0): This line checks if the given number is less than or equal to 1 using thecompareTomethod ofBigInteger. If it is, the method returnsfalse.for (BigInteger i = BigInteger.valueOf(2); i.compareTo(number.sqrt()) <= 0; i = i.add(BigInteger.ONE)): This line initializes a for loop that starts from 2 and iterates up to the square root of the given number using thesqrtmethod ofBigInteger. It checks if the number is divisible by any number between 2 and its square root. If it is, the method returnsfalse.public static void main(String[] args): This line declares the main method, which serves as the entry point of the program.BigInteger number = new BigInteger("29"): This line declares aBigIntegervariable namednumberand assigns it the value 29. You can change this value to test different numbers.if (isPrime(number)): This line checks if the given number is prime by calling theisPrimemethod. If it returnstrue, it prints that the number is prime. Otherwise, it prints that the number is not prime.
Real-Life Example and Usage
The prime number checking program can be used in various real-life scenarios, such as:
- Security systems: Prime numbers play a crucial role in encryption algorithms used in security systems. Checking whether a number is prime is essential for generating secure encryption keys.
- Mathematical calculations: Prime numbers are used in various mathematical calculations, such as finding the greatest common divisor, generating random numbers, and solving complex equations.
- Data validation: Prime number checking can be used to validate user input in applications where only prime numbers are allowed, such as generating prime numbers for a mathematical game or puzzle.
By understanding and implementing the prime number checking program, you can enhance your Java programming skills and apply them to solve real-world problems.
Contact
contact@howtodoworld.com
