Program to print first N Prime numbers

Given a number N, the task is to print the first N prime number from 1 to N.

Examples:

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

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

Approach:



  • First take the number N as input.
  • Then use a for loop to iterate the numbers from 1 to N
  • Then check for each number to be a prime number. If it is a prime number, print it.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to display first N Prime numbers
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print first N prime numbers
void print_first_N_primes(int N)
{
    // Declare the variables
    int i, j, flag;
  
    // Print display message
    cout << "\nPrime numbers between 1 and "
         << N << " are:\n";
  
    // Traverse each number from 1 to N
    // with the help of for loop
    for (i = 1; i <= N; i++) {
  
        // Skip 0 and 1 as they are
        // niether prime nor composite
        if (i == 1 || i == 0)
            continue;
  
        // flag variable to tell
        // if i is prime or not
        flag = 1;
  
        for (j = 2; j <= i / 2; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
  
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            cout << i << " ";
    }
}
  
// Driver code
int main()
{
  
    int N = 100;
  
    print_first_N_primes(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to display 
// first N Prime numbers 
class GFG 
{
      
// Function to print first N prime numbers 
static void print_first_N_primes(int N) 
    // Declare the variables 
    int i, j, flag; 
  
    // Print display message 
    System.out.println("Prime numbers between 1 and " 
                                      + N + " are:"); 
  
    // Traverse each number from 1 to N 
    // with the help of for loop 
    for (i = 1; i <= N; i++) 
    
  
        // Skip 0 and 1 as they are 
        // niether prime nor composite 
        if (i == 1 || i == 0
            continue
  
        // flag variable to tell 
        // if i is prime or not
        flag = 1
  
        for (j = 2; j <= i / 2; ++j) 
        
            if (i % j == 0)
            
                flag = 0
                break
            
        
  
        // flag = 1 means i is prime 
        // and flag = 0 means i is not prime 
        if (flag == 1
            System.out.print(i + " "); 
    
  
// Driver code 
public static void main (String[] args) 
    int N = 100
  
    print_first_N_primes(N); 
}
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to display first N Prime numbers
  
# Function to print first N prime numbers
def print_first_N_primes(N):
  
    # Declare the variables
    i, j, flag = 0, 0, 0;
  
    # Print display message
    print("Prime numbers between 1 and ",
                            N , " are:");
  
    # Traverse each number from 1 to N
    # with the help of for loop
    for i in range(1, N + 1, 1):
  
        # Skip 0 and 1 as they are
        # niether prime nor composite
        if (i == 1 or i == 0):
            continue;
  
        # flag variable to tell
        # if i is prime or not
        flag = 1;
  
        for j in range(2, ((i // 2) + 1), 1):
            if (i % j == 0):
                flag = 0;
                break;
  
        # flag = 1 means i is prime
        # and flag = 0 means i is not prime
        if (flag == 1):
            print(i, end = " ");
  
# Driver code
N = 100;
print_first_N_primes(N);
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to display 
// first N Prime numbers 
using System;
      
class GFG 
{
      
// Function to print first N prime numbers 
static void print_first_N_primes(int N) 
    // Declare the variables 
    int i, j, flag; 
  
    // Print display message 
    Console.Write("Prime numbers between 1 and "
                                   N + " are:\n"); 
  
    // Traverse each number from 1 to N 
    // with the help of for loop 
    for (i = 1; i <= N; i++) 
    
  
        // Skip 0 and 1 as they are 
        // niether prime nor composite 
        if (i == 1 || i == 0) 
            continue
  
        // flag variable to tell 
        // if i is prime or not
        flag = 1; 
  
        for (j = 2; j <= i / 2; ++j) 
        
            if (i % j == 0)
            
                flag = 0; 
                break
            
        
  
        // flag = 1 means i is prime 
        // and flag = 0 means i is not prime 
        if (flag == 1) 
            Console.Write(i + " "); 
    
  
// Driver code 
public static void Main (String[] args) 
    int N = 100; 
  
    print_first_N_primes(N); 
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

Prime numbers between 1 and 100 are:
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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Rajput-Ji