Today we will learn C program to print prime numbers from 1 to n. So before starting you should have a little bit of knowledge about prime numbers.

### What are Prime Numbers?

Any natural number which can be divisible by itself or the number which has only two factors i.e 1 and the number itself are called prime numbers.

For example 2,3,5,7,11 and so on…

Note: The number 2 is only even prime number because most of the numbers are divisible by 2.

There are different methods to print prime numbers from 1 to n.We will see it one by one.

## 1. C program to print numbers from 1 to n using for loop

In this program, the compiler will ask the user to enter the number to which the user wants to print prime numbers using for loop.

```#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number till which you want prime numbers\n");
scanf("%d",&n);
printf("Prime numbers are:-\n");
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);
}
}
}```

Output:

## 2. C program to Print Prime Numbers from 1 to n using while loop

In this program, the compiler will ask user to enter the minimum and maximum number from which user wants to print all prime numbers between minimum and maximum using while loop.

```#include <stdio.h>
int main()
{
int i, Number, count, Minimum, Maximum;
printf("\n Please Enter the Minimum & Maximum Values\n");
scanf("%d %d", &Minimum, &Maximum);
Number = Minimum;
printf("Prime Numbers Between %d and %d are:\n", Minimum, Maximum);
while(Number <= Maximum)
{
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;
}```

Output:

In the above program, we have used while loop instead of for loop. Don’t be afraid program is simple and the logic behind the program is the same.

## 3. C program to print Prime Numbers between 1 to 100 using for loop

```#include <stdio.h>
int main()
{
int i, Number, count;
printf(" Prime Number from 1 to 100 are: \n");
for(Number = 1; Number <= 100; Number++)
{
count = 0;
for (i = 2; i <= Number/2; i++)
{
if(Number%i == 0)
{
count++;
break;
}
}
if(count == 0 && Number != 1 )
{
printf(" %d ", Number);
}
}
return 0;
}```

Output:

## 4. C program to print Prime Numbers between 1 to 100 using While loop

```#include <stdio.h>
int main()
{
int i, Number = 1, count;
printf(" Prime Number from 1 to 100 are: \n");
while(Number <= 100)
{
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;
}```

Output:

## 5. Program to calculate the sum of prime number from 1 to 100

In this program, the compiler will calculate and print the addition of prime numbers from 1 to 100

```#include <stdio.h>
int main()
{
int i, Number, count, Sum = 0;
for(Number = 1; Number <= 100; Number++)
{
count = 0;
for (i = 2; i <= Number/2; i++)
{
if(Number%i == 0)
{
count++;
break;
}
}
if(count == 0 && Number != 1 )
{
Sum = Sum + Number;
}
}
printf(" Sum of Prime Numbers between 1 to 100 =  %d", Sum);
return 0;
}```

Output:

## 6. Program to calculate the sum of prime number from 1 to n

```#include <stdio.h>
int main()
{
int i, Number, count, Sum = 0, Minimum, Maximum;
printf("\n Please Enter the Minimum & Maximum Values\n");
scanf("%d %d", &Minimum, &Maximum);
for(Number = Minimum; Number <= Maximum; Number++)
{
count = 0;
for (i = 2; i <= Number/2; i++)
{
if(Number%i == 0)
{
count++;
break;
}
}
if(count == 0 && Number != 1 )
{
Sum = Sum + Number;
}
}
printf(" Sum of Prime Numbers between %d and %d =  %d", Minimum, Maximum, Sum);
return 0;
}```

Output: