C program and algorithm to print prime numbers upto a limit

Algorithm

  1. start
  2. set i=1,j=1
  3. read limit
  4. if limit =1
  5. display one is neither prime nor composite
  6. else
  7. repeat steps 8 to 14 if i<=limit,count=0
  8. repeat steps 9 to 11 if j<=i
  9. if i%j==0
  10. count++
  11. j+1 go to step 8
  12. if count==2
  13. display i
  14. i+1 go to step 4
  15. else
  16. stop
Program

#include<stdio.h>
#include<conio.h>   //only for windows users
void main()
{
clrscr();   //only for windows users
int lim,i,j,count;
printf("\n enter the limit:\t");
scanf("%d",&lim);
if(lim==1)
{
printf("\n one is neither prime nor composite");
}
else
for(i=1;i<=lim;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
}
getch();   \\only for windows users
}

Output

enter the limit: 10
2 3 5 7

1 comment: