August 11, 2022 |16.6K Views

C Program to Print Prime Numbers from 1 to N.

  Share  1 Like
Description
Discussion

A prime number is a number greater than 1 that can only be divided by 1 and itself without a remainder. For example, prime numbers between 1 and 100 include 2, 3, 5, 7, and so on. To print all prime numbers up to a given number NNN, the algorithm takes input NNN, iterates through numbers from 1 to NNN, and checks each for primality. If a number is prime, it is printed. This method has a time complexity of O(n2/3)O(n^{2/3})O(n2/3) and a space complexity of O(1)O(1)O(1).

For instance, given N=10N = 10N=10, the output is 2, 3, 5, 7, while for N=5N = 5N=5, the output is 2, 3, 5. This tutorial explains the step-by-step implementation of this algorithm in C. 

For more details, please go through - C Program to Print Prime Numbers From 1 to N