Monday, September 10, 2012

Prime Numbers Program

Write a program to find prime numbers upto entered number in shortest number of loops


Solution:


#include<stdio.h>
main()
{
int a,b,c,d;
clrscr();
printf("Enter number upto which you want prime numbers:\n");
scanf("%d",&a);
 for(c=2;c<a;c++)//c=2 for excluding 1
  {
for(b=1,d=0;b*b<=c;b++)//d must be initialised every time
{
if(c%b==0)
d++;
}
if(d==1)
printf("%d\t",c);
  }
getch();
return(0);
}

No comments:

Post a Comment