EX.NO: 4 C program to generate all prime numbers from 1 to N.
DATE:
AIM
To write a C program to generate all prime numbers from 1 to N.
ALGORITHM:
Step 1: Start
Step 2: Initialize variables num=1, N, flag=1, j=2
Step 3: Read N from user
Step 4: Repeat the steps 4 to 7 until num<N
flag=1;
Step 5: Repeat the steps until j<[(num/2)+1]
If ((num%j)==0)
Set flag=0
Goto step 7
j=j+1
Step 6: If flag==1
Display num
Step 7: num=num+1;
Step 8: Stop
PROGRAM :
#include <stdio.h>
#include <conio.h>
void main() {
int N, f;
clrscr();
printf("Enter the value of N: ");
scanf("%d", &N);
printf ("Prime numbers from 1 to %d are:\n", N);
for (int i = 1; i <= N; i++) {
f=0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0)
f=1;
}
if(f==0)
printf("%d ", i);
}
getch();
}
RESULT:
Thus, the C program to generate all prime numbers from 1 to N was
executed successfully