Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
Prime Number Program in Java (Code to Check Prime or Not)
Prime numbers are a fundamental concept in mathematics. As Java developers, it's essential to understand different techniques to identify them efficiently. In this tutorial, you will learn about the Java program for prime numbers using different ways.
We have covered different ways to check number is prime or not in Java, as well as print prime numbers in a given range. For instance, you can use the loops in Java (for loop, while loop), and command line arguments to find the prime number.
So, let’s get started and practice the prime number program in Java language.
Concepts to Learn for This Program:
Check Number is Prime or Not in Java
The following program takes user input and checks if the entered number is a prime or not:
Code
import java.util.Scanner;
public class CheckPrime {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();
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;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
}
return true;
}
}
Output
Enter a number: 87
Explanation
In this program, we use a Scanner to read the user's input from the console. The program prompts the user to enter a number, and the entered value is stored in the number variable.
We then call the isPrime() method to check if the entered number is prime or not in Java using Scanner.
Based on the result of the prime check, the program prints whether the entered number is a prime number or not.
Prime Numbers Program in Java Using for loop
Here's a Java program to print prime numbers within a given range using a for loop:
Code
public class PrimeNumbersInRange {
public static void main(String[] args) {
int startRange = 2; // Change this to your desired start of the range
int endRange = 50; // Change this to your desired end of the range
System.out.println("Prime numbers between " + startRange + " and " + endRange + ":");
for (int number = startRange; number <= endRange; number++) {
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.print(number + " ");
}
}
}
}
Output
Prime numbers between 2 and 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Explanation
In this program, we define the startRange and endRange variables to specify the range of numbers for which we want to find the prime numbers in Java. The program then uses a for loop to iterate through each number in the range.
For each number, the program uses a similar prime checking logic. It checks if the number is less than or equal to 1 and marks it as not prime. Otherwise, it iterates from 2 to the square root of the number to check for divisibility.
If the number is divisible by any integer in that range, it's not prime, and the isPrime variable is set to false. Otherwise, it remains true, indicating the number is prime.
If the isPrime variable is true, the number is printed as part of the output, showing all the prime numbers within the specified range.
Prime Number Program in Java Using while loop
Here's a Java program to print prime numbers within a given range using a while loop:
Code
public class PrimeNumbersInRange {
public static void main(String[] args) {
int startRange = 2; // Change this to your desired start of the range
int endRange = 50; // Change this to your desired end of the range
System.out.println("Prime numbers between " + startRange + " and " + endRange + ":");
int number = startRange;
while (number <= endRange) {
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
int i = 2;
while (i <= Math.sqrt(number)) {
if (number % i == 0) {
isPrime = false;
break;
}
i++;
}
}
if (isPrime) {
System.out.print(number + " ");
}
number++;
}
}
}
Output
Prime numbers between 2 and 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Explanation
In this program, we define the startRange and endRange variables to specify the range of numbers for which we want to find the prime numbers. The program then uses a while loop to iterate through each number in the range.
Inside the loop, we use a similar prime checking logic as in the previous programs. It checks if the number is less than or equal to 1 and marks it as not prime. Otherwise, it uses another while loop with i starting from 2 and iterating until i is less than or equal to the square root of the number.
If the isPrime variable is true, the number is printed as part of the output, showing all the prime numbers within the specified range.
Java Program for Prime Number Between 1 to 100
Here's a Java program to find all prime numbers between 1 and 100:
Code
public class PrimeNumbersInRange {
public static void main(String[] args) {
int startRange = 1;
int endRange = 100;
System.out.println("Prime numbers between " + startRange + " and " + endRange + ":");
for (int number = startRange; number <= endRange; number++) {
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.print(number + " ");
}
}
}
}
Output
Prime numbers between 1 and 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Explanation
In this program, we use a for loop to iterate through each number from 1 to 100. For each number, we use a similar prime checking logic as in the previous examples to determine if it's a prime number.
If the isPrime variable is true, the number is printed as part of the output, showing all the prime numbers between 1 and 100.
Prime Number Program in Java Using Command Line Arguments
Here's a Java program to find and print all prime numbers using command-line arguments:
Code
public class PrimeNumbersInRange {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java PrimeNumbersInRange <startRange> <endRange>");
return;
}
int startRange = Integer.parseInt(args[0]);
int endRange = Integer.parseInt(args[1]);
System.out.println("Prime numbers between " + startRange + " and " + endRange + ":");
for (int number = startRange; number <= endRange; number++) {
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.print(number + " ");
}
}
}
}
Output
Prime numbers between 1 and 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Explanation
In this program, we utilize command-line arguments to specify the range of numbers for which we want to find the prime numbers. The program expects two arguments: and .
For example, you can run the program like this:
java PrimeNumbersInRange 1 100
The args array in the main method holds the command-line arguments. We check if there are exactly two arguments provided; otherwise, we display the correct usage of the program and exit.
After parsing the startRange and endRange integers from the command-line arguments, the program proceeds to find and print all prime numbers between the specified range, using the same prime checking logic as in the previous examples.
Learn Next: