To print all prime numbers from 1 to n using C program can easily be implemented with for loop. All we need to do is to run a loop from 1 to n and check each number, whether it is divisible by any number except 1 and itself. If it is divisible by any number in between then it is not prime or else it is a prime number.

Steps to Print Prime Number from 1 to N in C

  1. Run a loop from 1 to N (i).
  2. Another loop from 2 to less than i (j).
  3. Then inside loop check, if i is divisible by j then it means that it has a factor and thus not a prime number. Therefore break the nested inner loop.
  4. If the nested loop is over after completing all its iteration successfully, then it means that the number i is prime.
  5. Print i.

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

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

Close Menu