Home » C Programs » C Program to Print Prime Numbers From 1 to n

C Program to print prime numbers from 1 to n. A prime number is a number larger than one that is not the product of two lesser natural numbers. A composite number is a natural number greater than 1 that is not prime.

We utilise two for loops, one for counting up to n and the other for checking whether the integer is prime or not. The technique is therefore the same as determining if an integer is prime or not.

A for loop is a repetition control structure that allows us to create a loop that runs a set number of times. The loop allows us to do an arbitrary number of steps in a single line. A loop variable is used to regulate the loop in for loop.

A loop in C is made up of two parts: the loop body and the control statement. The control statement is a set of conditions that tells the loop’s body to keep running until the provided condition is false. The C loop’s goal is to repeat the same code several times. Using for loop we have successfully executed the c program to print prime numbers from 1 to n

```
//C Program to Print Prime Numbers From 1 to n
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number for prime number you want\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

Enter the number for prime number you want

45

Prime numbers are

2 3 5 7 11 13 17 19 23 29 31 37 41 43