Skip to content
Related Articles

Related Articles

Java Program to Display All Prime Numbers from 1 to N
  • Last Updated : 17 Mar, 2021

For a given number N, the purpose is to find all the prime numbers from 1 to N.

Examples:  

Input: N = 11
Output: 2, 3, 5, 7, 11
Input: N = 7
Output: 2, 3, 5, 7 

Approach 1:

  • Firstly, consider the given number N as input.
  • Then apply a for loop in order to iterate the numbers from 1 to N.
  • At last, check if each number is a prime number and if it’s a prime number then print it using brute-force method.

Java




// Java program to find all the
// prime numbers from 1 to N
class gfg {
  
    // Function to print all the
    // prime numbers till N
    static void prime_N(int N)
    {
        // Declaring the variables
        int x, y, flg;
  
        // Printing display message
        System.out.println(
            "All the Prime numbers within 1 and " + N
            + " are:");
  
        // Using for loop for traversing all
        // the numbers from 1 to N
        for (x = 1; x <= N; x++) {
  
            // Omit 0 and 1 as they are
            // niether prime nor composite
            if (x == 1 || x == 0)
                continue;
  
            // Using flag variable to check
            // if x is prime or not
            flg = 1;
  
            for (y = 2; y <= x / 2; ++y) {
                if (x % y == 0) {
                    flg = 0;
                    break;
                }
            }
  
            // If flag is 1 then x is prime but
            // if flag is 0 then x is not prime
            if (flg == 1)
                System.out.print(x + " ");
        }
    }
  
    // The Driver code
    public static void main(String[] args)
    {
        int N = 45;
  
        prime_N(N);
    }
}
Output
All the Prime numbers within 1 and 45 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 

Time Complexity: O(N2)



Approach 2:

  • Firstly, consider the given number N as input.
  • Then apply a for loop in order to iterate the numbers from 1 to N.
  • At last, check if each number is a prime number and if it’s a prime number then print it using the square root method.

Java




// Java program to find all the
// prime numbers from 1 to N
class gfg {
  
    // Function to print all the
    // prime numbers till N
    static void prime_N(int N)
    {
        // Declaring the variables
        int x, y, flg;
  
        // Printing display message
        System.out.println(
            "All the Prime numbers within 1 and " + N
            + " are:");
  
        // Using for loop for traversing all
        // the numbers from 1 to N
        for (x = 2; x <= N; x++) {
  
            // Using flag variable to check
            // if x is prime or not
            flg = 1;
  
            for (y = 2; y * y <= x; y++) {
                if (x % y == 0) {
                    flg = 0;
                    break;
                }
            }
  
            // If flag is 1 then x is prime but
            // if flag is 0 then x is not prime
            if (flg == 1)
                System.out.print(x + " ");
        }
    }
  
    // The Driver code
    public static void main(String[] args)
    {
        int N = 45;
  
        prime_N(N);
    }
}
Output
All the Prime numbers within 1 and 45 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 

Time Complexity: O(N3/2)

Approach 3:

Java




// Java program to print all
// primes smaller than or equal to
// n using Sieve of Eratosthenes
  
class SieveOfEratosthenes {
    void sieveOfEratosthenes(int n)
    {
        // Create a boolean array
        // "prime[0..n]" and
        // initialize all entries
        // it as true. A value in
        // prime[i] will finally be
        // false if i is Not a
        // prime, else true.
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            prime[i] = true;
  
        for (int p = 2; p * p <= n; p++) {
            // If prime[p] is not changed, then it is a
            // prime
            if (prime[p] == true) {
                // Update all multiples of p
                for (int i = p * p; i <= n; i += p)
                    prime[i] = false;
            }
        }
  
        // Print all prime numbers
        for (int i = 2; i <= n; i++) {
            if (prime[i] == true)
                System.out.print(i + " ");
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
        int N = 45;
        System.out.println(
            "All the Prime numbers within 1 and " + N
            + " are:");
        SieveOfEratosthenes g = new SieveOfEratosthenes();
        g.sieveOfEratosthenes(N);
    }
}
Output
All the Prime numbers within 1 and 45 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 

Time complexity : O(n*log(log(n))) 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :