# Program to print prime numbers from 1 to N.

Given a number N, the task is to print the prime numbers from 1 to N.**Examples:**

Input:N = 10Output:2, 3, 5, 7Input:N = 5Output: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_primes_till_N(` `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` ` ` `// neither 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_primes_till_N(N);` ` ` `return` `0;` `}` |

## Java

`// Java program to display` `// first N Prime numbers` `class` `GFG` `{` ` ` `// Function to print first N prime numbers` `static` `void` `print_primes_till_N(` `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` ` ` `// neither 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_primes_till_N(N);` `}` `}` `// This code is contributed by AnkitRai01` |

## Python3

`# Python3 program to display first N Prime numbers` `# Function to print first N prime numbers` `def` `print_primes_till_N(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` ` ` `# neither 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_primes_till_N(N);` `# This code is contributed by Rajput-Ji` |

## C#

`// C# program to display` `// first N Prime numbers` `using` `System;` ` ` `class` `GFG` `{` ` ` `// Function to print first N prime numbers` `static` `void` `print_primes_till_N(` `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` ` ` `// neither 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_primes_till_N(N);` `}` `}` `// This code is contributed by Rajput-Ji` |

## Javascript

`<script>` `// JavaScript program to display first N Prime numbers` `// Function to print first N prime numbers` `function` `print_primes_till_N(N)` `{` ` ` `// Declare the variables` ` ` `let i, j, flag;` ` ` `// Print display message` ` ` `document.write(` ` ` `"Prime numbers between 1 and "` `+ N + ` `" are:<br>"` ` ` `);` ` ` `// 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` ` ` `// neither 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)` ` ` `document.write(i + ` `" "` `);` ` ` `}` `}` `// Driver code` `let N = 100;` `print_primes_till_N(N);` `// This code is contributed by _saurabh_jaiswal` `</script>` |

**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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced 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****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.