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++
// 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
filter_none
Java
// 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
filter_none
Python3
# 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
filter_none
C#
// 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
filter_none
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
Recommended Posts:
- Python program to print all Prime numbers in an Interval
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Print prime numbers in a given range using C++ STL
- Print all prime numbers less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Print prime numbers from 1 to N in reverse order
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Efficient program to print all prime factors of a given number
- C Program for efficiently print all prime factors of a given number
- Program to print characters present at prime indexes in a given string
- Java Program for efficiently print all prime factors of a given number
- Program to find sum of prime numbers between 1 to n
- Program to find Prime Numbers Between given Interval
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.

