Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   problem with prime no's (http://www.go4expert.com/forums/prime-nos-t7055/)

rohanag 26Oct2007 13:05

problem with prime no's
 
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 26Oct2007 18:40

Re: problem with prime no's
 
c will always be greater than or equal to 2 because for 1 and num the condition is always true.

rohanag 26Oct2007 18:43

Re: problem with prime no's
 
ya, thanks, i got my problem

shabbir 26Oct2007 18:49

Re: problem with prime no's
 
Quote:

Originally Posted by rohanag
ya, thanks, i got my problem

kool

hbchen 26Oct2007 23:12

Re: problem with prime no's
 
I can not see your problem of the code. Why isn't 127 a prime?

rohanag 26Oct2007 23:48

Re: problem with prime no's
 
i know, 127 is prime,program is fine, sorry for the mistake


All times are GMT +5.5. The time now is 03:16.