C program to print prime numbers from 1 to N; Through this tutorial, we will learn how to print prime numbers from 1 to N(10, 100, 500, 1000, etc) in the c program using for loop, while loop, and recursion.

## Programs to Print Prime Numbers from 1 to N in C

- C Program to Print Prime Numbers from 1 to N Using For Loop
- C Program to Print Prime Numbers from 1 to N Using While Loop
- C Program to Print Prime Numbers from 1 to N Using Recursion

### C Program to Print Prime Numbers from 1 to N Using For Loop

```
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number till which you want prime numbers :- ");
scanf("%d",&n);
printf("Prime numbers are:- ");
for(i=2;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d ",i);
}
}
}
```

**The output of the above c program; as follows:**

Enter the number till which you want prime numbers :- 20 Prime numbers are:- 2 3 5 7 11 13 17 19

### C Program to Print Prime Numbers from 1 to N Using While Loop

```
#include <stdio.h>
int main()
{
int i, Number = 1, count, n;
printf("Enter the number till which you want prime numbers :- ");
scanf("%d", &n);
printf("Prime numbers are:- ");
while(Number <= n)
{
count = 0;
i = 2;
while(i <= Number/2)
{
if(Number%i == 0)
{
count++;
break;
}
i++;
}
if(count == 0 && Number != 1 )
{
printf(" %d ", Number);
}
Number++;
}
return 0;
}
```

**The output of the above c program; as follows:**

Enter the number till which you want prime numbers :- 20 Prime numbers are:- 2 3 5 7 11 13 17 19

### C Program to Print Prime Numbers from 1 to N Using Recursion

```
#include<stdio.h>
#include<math.h>
int CheckPrime(int i,int num)
{
if(num==i)
return 0;
else
if(num%i==0)
return 1;
else{
return CheckPrime(i+1,num);
}
}
int main()
{
int n,i;
printf("Enter the N Value:");
scanf("%d",&n);
printf("Prime Number Between 1 to n are: ");
for(i=2;i<=n;i++)
if(CheckPrime(2,i)==0)
printf("%d ",i);
}
```

**The output of the above c program; as follows:**

Enter the N Value:20 Prime Number Between 1 to n are: 2 3 5 7 11 13 17 19