# Write a C Program to Generate All Prime Numbers Between 1 and N

In this article, we will write a C program to generate all prime numbers between 1 and N.

A prime number is a number that is exactly divisible by one and itself only.

# ALGORITHM:

Step 1: start

Step 3: initialize i=1,c=0

Step 4:if i<=n goto step 5 If not goto step 10

Step 5: initialize j=1

Step 6: if j<=i do the following. If no goto step 7

i)if i%j==0 increment c

ii) increment j

iii) goto Step 6

Step 7: if c== 2 print I

Step 8: increment I

Step 9: goto step 4

Step 10: stop

# Write a C Program to Generate All Prime Numbers Between 1 and N

``````#include <stdio.h>
#include <conio.h>

void main()
{
int n, i, fact, j;
clrscr();
printf("enter the number:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact = 0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for (j = 1; j <= i; j++)
{
if (i % j == 0)
fact++;
}
if (fact == 2)
printf("\n %d", i);
}
getch();
}
``````

Output:

``````Enter the number : 5
2 3 5 ``````