problem with prime no's

rohanag's Avatar, Join Date: Oct 2007
Light Poster
i want to print prime no's between 2 and 200,
in te code i've written, it is also printing 127 which is not prime, don't know why because the variable c should be greater than 2 for 127

Code: c
#include<conio.h>
#include<stdio.h>
#include<math.h>
int main()
{
int c,i,num;
clrscr();
for (num=2;num<=200;num++)
{
 c=0;
 for(i=1;i<=num;i++)
 {
 if(num%i==0) c=c+1;        //checking if num is divisible by i
 }
 if(c==2)            //for prime no's c will be 2
 printf("%d ",num);    //num is div by 1 and num itself
}
getch();
return 0;
}
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
c will always be greater than or equal to 2 because for 1 and num the condition is always true.
rohanag's Avatar, Join Date: Oct 2007
Light Poster
ya, thanks, i got my problem
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by rohanag
ya, thanks, i got my problem
kool
hbchen's Avatar, Join Date: Oct 2007
Newbie Member
I can not see your problem of the code. Why isn't 127 a prime?
rohanag's Avatar, Join Date: Oct 2007
Light Poster
i know, 127 is prime,program is fine, sorry for the mistake