**C program to print prime numbers upto n** is the one of the most important programs in c.

### What is prime number?

Any number which is divisible only by one and itself. **two (2) is the smallest even prime number**.

Example: **2, 3, 5, 7, 11, 13, 17,…..**

A number greater than one can be factorized into prime numbers, for example, **540 = 2 ^{2}*3^{3}*5^{1}**

### Logic to print number till N

We take a number input from a user. next, we check whether a number is divisible by 1 and itself only if this is true then print number.

## C program to print prime numbers upto n

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 |
#include<stdio.h> int main() { int n, i = 3, count, c; printf("Enter the number of prime numbers required\n"); scanf("%d",&n); if ( n >= 1 ) { printf("First %d prime numbers are :\n",n); printf("2\n"); } for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n", i); count++; } i++; } return 0; } |

**Output :**

## Leave a Reply