C program To Print All The prime numbers between 1 And N :-
Algorithm:
Step 1: start
Step 2: read n
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 <= 1 do as the follow. 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
Flow Chart :-
Input & Output:
Prime no. series
Enter any number
10
The prime numbers between 1 to 10
2 3 5 7
C source code:-
#include<stdio.h>
#include<conio.h>
int
main(){
int
N, i, j, isPrime, n;
printf
(
"To print all prime numbers between 1 to N\n"
);
printf
(
"Enter the value of N\n"
);
scanf
(
"%d"
,&N);
printf
(
"Prime numbers between %d to %d\n"
, 1, N);
for
(i = 2; i <= N; i++){
isPrime = 0;
for
(j = 2; j <= i/2; j++){
if
(i % j == 0){
isPrime = 1;
break
;
}
}
if
(isPrime==0 && N!= 1)
printf
(
"%d "
,i);
}
getch();
return
0;
}
Comments
Post a Comment