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

- Run a loop from 1 to N (i).
- Another loop from 2 to less than i (j).
- 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.
- If the nested loop is over after completing all its iteration successfully, then it means that the number i is prime.
- Print i.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() { int n, i, j; bool isPrime; printf("Enter value of n \n"); scanf("%d",&n); printf("Prime Number between 1 to %d are: \n",n); printf("\n"); printf("%d, ",1); //Assuming 1 as prime, so printing it //Starting to check from two for(i=2; i<=n; i++){ isPrime = true; //Assuming that i is prime for(j=2; j<i; j++){ if(i%j == 0){ //If the number is divisible then //it is not prime isPrime = false; break; } } if(isPrime) printf("%d, ",i); //Will be executed if number is prime } return 0; } |

**Output for All Prime Numbers from 1 to 100**

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.