Examples
- Leap Year Program in Java (Check Leap Year or Not)
- Check Number is Positive or Negative in Java (4 Ways)
- Java Program to Check Character is Alphabet or Not
- Armstrong Number Program in Java (for loop, Recursion)
- Print Prime Numbers Between 1 to N in Java (1 to 100)
- Java Program for Palindrome Number (Palindrome Code)
- Sum of n Natural Numbers in Java (Programs & Explanation)
- Java Multiplication Table Program (Loops, 2D Array) 5 Ways
- Find GCD of Two Numbers in Java (HCF Program)
- GCD of Three Numbers in Java (HCF of 3 Numbers Program
- GCD of Array in Java (GCD of n Numbers Program)
- LCM of Two Numbers in Java (LCM Program and Code)
- LCM of Three Numbers in Java (Easy Programs)
- LCM of n Numbers in Java (LCM of Array of Numbers)
- How to Print A to Z in Java? 3 Ways to Print Alphabets
Print Prime Numbers Between 1 to N in Java (1 to 100)
Prime numbers, those elusive integers divisible only by 1 and themselves, have fascinated mathematicians and computer scientists for centuries. Their unique properties make them not only a subject of mathematical inquiry but also a valuable tool in various computational tasks.
In this tutorial, we will learn how to print prime numbers from 1 to N in Java.
Why Prime Numbers Matter?
Prime numbers hold a special place in mathematics and computer science for several reasons:
-
Cryptography: Prime numbers play a pivotal role in modern cryptography. Algorithms like RSA (Rivest-Shamir-Adleman) rely on the difficulty of factoring large semiprime numbers (the product of two prime numbers) to ensure the security of online transactions, data encryption, and more.
-
Data Security: Prime numbers also find applications in ensuring the integrity of data. Hash functions often use prime numbers to produce unique hash codes for data, making it difficult for malicious actors to manipulate or corrupt information.
-
Efficiency: In computer science, prime numbers are used in various algorithms and data structures to optimize performance. They help distribute data evenly, reducing collisions in hash tables and improving the efficiency of algorithms like quicksort and merge sort.
-
Random Number Generation: Many random number generators use prime numbers as part of their algorithms to produce pseudorandom sequences with desirable statistical properties.
-
Coding Challenges: Prime numbers are a frequent subject in coding challenges and competitive programming. Understanding how to work with prime numbers is essential for solving a wide range of problems efficiently.
So, we will equip you with the Java programs to print prime numbers from 1 to 100 or any number (N). Let's get started!
Concepts to Learn:
Print Prime Numbers from 1 to N in Java
We will print prime numbers in given range using the Brute Force method. This method works by testing each number in the given range individually for primality, making it a straightforward but less efficient approach for larger values of n.
Code
public class PrimeNumbers {
public static void main(String[] args) {
int n = 50; // Change 'n' to the desired range
System.out.println("Prime numbers from 1 to " + n + ":");
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
Output
Prime numbers from 1 to 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Explanation
-
We specify the value of n to determine the range of prime numbers we want to find.
-
We iterate through the numbers from 2 to n, as 1 is not considered a prime number.
-
For each number i in the range, we use a nested loop to check whether it is prime.
-
In the inner loop, we check if i is divisible by any number j from 2 to i - 1. If it is divisible, we set isPrime to false and break out of the inner loop.
-
If isPrime remains true after the inner loop, it means that i is prime, and we print it.
Print Prime Numbers from 1 to 100 in Java
This program efficiently identifies and prints prime numbers from 1 to 100 by using the square root of each number as the upper limit for checking divisors, reducing the number of divisor checks needed.
Here's how to print prime numbers from 1 to 100 in Java:
Code
public class PrimeNumbers {
public static void main(String[] args) {
int n = 100;
System.out.println("Prime numbers from 1 to " + n + ":");
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
Output
Prime numbers from 1 to 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
-
We specify n as 100 to determine the range of prime numbers we want to find.
-
We iterate through the numbers from 2 to n as 1 is not considered a prime number.
-
For each number i in the range, we use a nested loop to check whether it is prime.
-
In the inner loop, we check if i is divisible by any number j from 2 to the square root of i. Checking up to the square root is more efficient because if i has a divisor greater than its square root, it must also have a divisor smaller than its square root. Therefore, there's no need to check beyond the square root.
-
If i is found to be divisible by any j, we set isPrime to false and break out of the inner loop.
-
If isPrime remains true after the inner loop, it means that i is prime, and we print it.
Prime Number Between Given Range in Java
Here's how to find and print prime numbers within a given range in Java:
Code
public class PrimeNumbersInRange {
public static void main(String[] args) {
int start = 10; // Change the start of the range
int end = 50; // Change the end of the range
System.out.println("Prime numbers between " + start + " and " + end + ":");
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
System.out.print(num + " ");
}
}
}
// Function to check if a number is prime
static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
Output
Prime numbers between 10 and 50:
11 13 17 19 23 29 31 37 41 43 47
Explanation
-
We specify the range by setting start and end to the desired values.
-
We iterate through the numbers from start to end, inclusive.
-
For each number in the range, we call the isPrime function to check if it is prime.
-
The isPrime function checks if a number is less than or equal to 1 and returns false in such cases. Otherwise, it iterates from 2 to the square root of the number and checks if the number is divisible by any integer in that range. If it finds a divisor, it returns false; otherwise, it returns true.
-
If the isPrime function returns true for a number in the range, we print that number as it is a prime number.
Print Prime Numbers from 1 to N in Java Using Trial Division
Here's a Java program to print prime numbers from 1 to n in Java using the Trial Division:
Code
public class PrimeNumbers {
public static void main(String[] args) {
int n = 500; // Change 'n' to the desired range
System.out.println("Prime numbers from 1 to " + n + ":");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
// Function to check if a number is prime
static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
if (num <= 3) {
return true;
}
if (num % 2 == 0 || num % 3 == 0) {
return false;
}
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
}
Output
Prime numbers from 1 to 500:
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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
Explanation
-
We specify n as 500 to determine the range of prime numbers we want to find.
-
We iterate through the numbers from 2 to n as 1 is not considered a prime number.
-
For each number i in the range, we call the isPrime function to check if it is prime.
-
The isPrime function checks if a number is less than or equal to 1, and if so, returns false. It then checks if the number is less than or equal to 3 and returns true for these cases. It also checks if the number is divisible by 2 or 3; if it is, it returns false.
-
The main part of the isPrime function uses a trial division approach with a loop that starts from 5 and increments by 6 in each iteration. This approach efficiently checks for divisibility by numbers of the form 6k ± 1, where k is an integer. We only check up to the square root of the number.
-
If none of the conditions in the isPrime function return false, it means the number is prime, and we return true.
Learn Next: