having problem with a simple loop

This is a discussion on having problem with a simple loop within the C Programming forums, part of the General Programming Boards category; Hello All, My instructor gave me a question to solve , its really simple but it keeps displaying me 0 ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    having problem with a simple loop

    Hello All,
    My instructor gave me a question to solve , its really simple but it keeps displaying me 0
    the questions says :
    Write a C program to Calculate and display the product of the even integers from 2 to 50.

    here is my work and i dunno why it displays 0.
    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    int i=2,P;
      while(i<50)
     {
       i=i+2;
       P=P*i;
     }
    printf("the product is %d",P);
    getch();
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    You didn't initialize P - right now, P just happens to be whatever is in memory when you declared it, and that value happened to be 0 here.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The product of even integers from 2 to 50 is 2^25 * 25! I believe which FAR exceeds the capacity of an integer. 2^20 itself is over 1 million! You may want to consider approximating that value or using some larger primitive type.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    mm kk i corrected it and made it double instead of int but idk why the instructor put it in int if it wont be able to work that way ? weired , anyways thanks for the help, appreciate it

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    From Claudiu's calculation: 2 ^ 25 * 25 = 838 860 800
    The upper range of a signed integer on most 32 bit machines: 2 ^ 31 - 1 = 2 147 483 647

    It should fit.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    25! not just 25.

    Think about it. There are 25 even numbers. If you extract the 2 factor from each one you end up with 2^25. The rest is the product of 1*2*3...*25.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Oh lol, my bad. I thought that was just enthusiasm or awe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help! short while loop tracking problem.
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 04-22-2010, 12:29 PM
  2. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  3. simple loop problem
    By scotty lee in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2007, 06:41 PM
  4. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21