
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display All Prime Numbers from 1 to N in Java
In this article, we will understand how to display all the prime numbers from 1 to N in Java. All possible positive numbers from 1 to infinity are called natural numbers.
A number is a prime number if its only factors are 1 and itself and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers.
Below is a demonstration of the same -
Input: Suppose our input is ? Enter the value of n :10 Output: The output would be- 2 3 5 7
Approach 1:
In this approach, we check each number from 2 to n and count how many times it is divisible without a remainder. If a number is divisible exactly twice (only by 1 and itself), it is printed as a prime number.
Algorithm
Step 1- Start Step 2- Declare an integer variable N and assign a value to it (e.g., N = 10) Step 3- Using a while loop from 1 to n, check if the 'i' value is divisible by any number from 2 to i. Step 4- If yes, check the next number Step 5- If no, store the number as a prime number Step 6- Display the 'i' value as LCM of the two numbers Step 7- Stop
Example 1
Here, the input is being entered by the user based on a prompt.
import java.util.Scanner; public class PrimeNumbers{ public static void main(String arg[]){ int i,n,counter, j; Scanner scanner = new Scanner(System.in); System.out.print("Enter the n value : "); n=scanner.nextInt(); System.out.print("Prime numbers between 1 to 10 are "); for(j=2; j<=n;j++){ counter=0; for(i=1;i<=j;i++){ if(j%i==0){ counter++; } } if(counter==2) System.out.print(j+" "); } } }
On compiling, the above program gives you the following output.
Enter the n value is 10 Prime numbers between 1 to 10 are 2 3 5 7
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class PrimeNumbers{ public static void main(String arg[]){ int i,n,counter, j; n= 10; System.out.printf("Enter the n value is %d ", n); System.out.printf("\nPrime numbers between 1 to %d are ", n); for(j=2;j<=n;j++){ counter=0; for(i=1;i<=j;i++){ if(j%i==0){ counter++; } } if(counter==2) System.out.print(j+" "); } } }
Output
Enter the n value is 10 Prime numbers between 1 to 10 are 2 3 5 7
Approach 2:
In this approach, instead of checking all numbers from 1 to x, it only checks divisibility from 2 up to underroot x (square root of x). If a number x has any factor greater than its square root, it also has a smaller factor less than or equal to underoot x, so checking up tounderroot x will find out if x is prime.
Algorithm
Step 1 - Start Step 2 - Declare an integer variable N and assign a value to it (e.g., N = 45) Step 3 - Declare integer variables: x (to check each number from 1 to N), y (to check divisibility for x), and flg (to mark whether x is prime or not). Step 4 - Start a for loop from x = 1 to x = N. This loop checks each number between 1 and N one by one. Step 5 - Inside the loop, check if x is 1. If yes, skip the current iteration (1 is not a prime number) Step 6 - Set flg = 1, assume the number x is prime for now. Step 7 - Start an inner for loop from y = 2 to y*y <= x. Step 8 - Inside the inner loop, check if x % y == 0, If yes, then: Set flg = 0 (x is not a prime number) and exit the inner loop using 'break`. Step 10 - After the inner loop ends, check if flg is still 1. If yes, then x is a prime number print x. Step 11 - Continue to the next value of x in the outer loop. Step 12 - Repeat Steps 6 to 11 until x equals N. Step 13 - Stop.
Example
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class PrimeNumbersFrom1ToN { public static void main(String[] args) { int N = 45; System.out.println("All the Prime numbers within 1 and " + N + " are:"); int x, y, flg; for (x = 1; x <= N; x++) { if (x == 1) { continue; } flg = 1; for (y = 2; y * y <= x; y++) { if (x % y == 0) { flg = 0; break; } } if (flg == 1) { System.out.print(x + " "); } } } }
On compiling, the above program gives you the following output.
All the Prime numbers within 1 and 45 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43