• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
August 11, 2022 |2.9K Views
C Program to Print Prime Numbers from 1 to N.
  Share  1 Like
Description
Discussion

In this video, we will write a C Program to print all the prime numbers from 1 to N. 

What is a prime number?
A prime number is a number that can only be divided by itself and 1 without remainders”. There are some examples of prime numbers from 1 to 100 are as follows: 
[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] 

Algorithm to print prime numbers from 1 to N: 
Step 1: First, take the input N number from the user. 
Step 2: Then use an iterative method to iterate the numbers from 1 to N and check each number to be a prime number. 
Step 3: Print the all is a prime number up to N. 

Examples
Input number: N = 10 
Output prime numbers: 2, 3, 5, 7 

Input number : N = 5 
Output prime numbers: 2, 3, 5 

Here time and space complexity for this method is O(n^2/3) & O(1).

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

Read More