Problem: Write a program in C to print all prime numbers from 1 to n.

A Prime number is a number that is divisible only by itself and 1. For example: 5.

To check and print all the prime numbers from 1 to N in C we need to follow the following steps:

  1. Loop from 1 to N (i).
  2. Nest another loop from 2 to less than i (j).
  3. Inside nested loop check if(i%j == 0). If true then i is not a prime number. Therefore, break out of the nested loop.
  4. Otherwise, if the iteration loops till the end, i is prime.
  5. Print i.

Note: The value of n should be entered by the user and should be positive.

Here is the implementation of the steps in C.

Output for All Prime Numbers from 1 to 100

C Program to Print Prime Numbers From 1 to N

That’s all we need to do to print all prime numbers for a given range using C program. If you have any doubt then do comment below.

Leave a Reply