Prime Number Program in Java
When it comes to programming in Java, understanding how to check for prime numbers is a common task that strengthens your problem-solving skills. Whether you’re preparing for coding interviews or brushing up on your basic Java knowledge, writing a Prime Number Program in Java is a great place to start.
In this article, we will discuss what prime numbers are, how to identify them, and how to write a simple yet efficient Prime Number Program in Java. Let’s break it down.
What is a Prime Number?
Before jumping into the code, it’s essential to understand what a prime number is.
- A prime number is a number greater than 1.
- It has only two factors: 1 and itself.
- Examples of prime numbers: 2, 3, 5, 7, 11, 13, 17, etc.
- 1 is not considered a prime number.
Key Points to Remember
When writing a Prime Number Program in Java, keep the following in mind:
- Prime numbers are only divisible by 1 and themselves.
- The number 2 is the smallest and only even prime number.
- For any number
n, you only need to check up to √n to determine if it's prime. - Optimization is key for checking larger numbers.
Basic Prime Number Program in Java
Here’s a simple Java program to check if 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 num = scanner.nextInt();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}Explanation:
- The user inputs a number.
- We check if the number is less than or equal to 1, in which case it’s not prime.
- For numbers greater than 1, the program checks divisibility up to the square root of the number.
- If a divisor is found, the number is not prime.
This simple approach forms the foundation of a basic Prime Number Program in Java.
Optimized Prime Number Program in Java
To make your program more efficient, especially when checking multiple numbers, you can use a function:
public class PrimeUtils {
public static boolean isPrime(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
for (int i = 1; i <= 50; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
}Advantages of This Version:
- Code reuse via
isPrime()function. - Skips even numbers after checking 2.
- Efficient for large number ranges.
This is a more advanced Prime Number Program in Java, which can be used in larger applications or during coding tests.
Real-World Applications
Understanding and implementing the Prime Number Program in Java helps in many real-world scenarios, such as:
- Cryptography and data security
- Hashing algorithms
- Game development (for random generation)
- Mathematics-based simulations
Final Thoughts
Writing a Prime Number Program in Java not only sharpens your logical thinking but also prepares you for a wide range of technical interviews. Make sure you understand the logic behind checking primes and know how to optimize your code for better performance.
Get Chirag Chandel’s stories in your inbox
Join Medium for free to get updates from this writer.
Whether you’re a beginner or an intermediate coder, practice is the key. Try writing variations such as printing all prime numbers in a range or counting them to deepen your understanding.
Want to explore more examples and variations of Prime Number Program in Java? Check out our complete guide on PrepInsta’s Prime Number Java Programs and start practicing today!










