1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

problem with prime no's

Discussion in 'C' started by rohanag, Oct 26, 2007.

  1. rohanag

    rohanag New Member

    Joined:
    Oct 21, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Studying
    Location:
    Jaipur
    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:
    #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;
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    c will always be greater than or equal to 2 because for 1 and num the condition is always true.
     
  3. rohanag

    rohanag New Member

    Joined:
    Oct 21, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Studying
    Location:
    Jaipur
    ya, thanks, i got my problem
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    kool
     
  5. hbchen

    hbchen New Member

    Joined:
    Oct 15, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I can not see your problem of the code. Why isn't 127 a prime?
     
  6. rohanag

    rohanag New Member

    Joined:
    Oct 21, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Studying
    Location:
    Jaipur
    i know, 127 is prime,program is fine, sorry for the mistake
     

Share This Page