VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-print-prime-numbers-from-1-to-n/

⇱ C Program to Print Prime Numbers From 1 to N - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Print Prime Numbers From 1 to N

Last Updated : 23 Jul, 2025

Prime numbers are positive integers greater than 1 that have no divisors other than 1 and themselves. In this article, we will learn to generate and display all prime numbers from 1 to N in C programming language.

👁 prime numbers under 100
Prime Numbers from 1 to 100

Algorithm

  • Check every number from 1 to N whether it is prime by using isPrime() function.
  • In isPrime() Function,
    • Iterate from 2 to sqrt(N) and check if the number is divisible by any of the values other than itself.
    • If it is divisible by any number, it means the number is not prime, return false.
    • If it is not divisible by any number, it means the number is prime, return true.
  • If isPrime() returns true, print the number.
  • Else continue.

Program to Print Prime Numbers From 1 to N in C


Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 

Time Complexity: O(N * √N)
Space Complexity: O(1)

Refer to the complete article Program to print prime numbers from 1 to N for more methods to generate prime numbers.

Related Articles

Comment
Article Tags: