Thread: simple question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    simple question

    I am a beginning programmer and this has been bugging me for a long time today, I can not see where I made an error, this program is suppose to return the prodoct of all even numbers from 2-100, but it only returns 0, I am just kind of brain dead that I can not find where I messed up, I know it is just simple mistake somewhere.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x, product;
    
        product = 1;
        for ( x = 2; x <= 100; x += 2)
            product = x * product;
    
        printf("the product is %d\n", product);
    
        return 0;
    }
    any help would be very appreciated

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Change your code to show what is happening, you should see the error:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x, product;
    
        product = 1;
        for ( x = 2; x <= 100; x += 2)
        {
            product = x * product;
            printf("product is now %d\n", product);
        }
    
        printf("the product is %d\n", product);
    
        return 0;
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps it overflows the range of integer values long before 100 is reached?

    [edit]D'oh! Pokey.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Dave_Sinkula
    Perhaps it overflows the range of integer values long before 100 is reached?

    [edit]D'oh! Pokey.
    yes this is exactly the problem, Ive tested with smaller values and it works here.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yeah, if you remember your times tables, everyone knows that the product of even numbers between 2 and 100 is 34243224702511976248246432895208185975118675053719 198827915654463488000000000000, far too big to be accurately stored in any native C type. Even the long double type results in accuracy loss when x reaches 52 on my implementation.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What's the maximum number that a long long can hold?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by dwks
    What's the maximum number that a long long can hold?
    LLONG_MAX, which must be at least +9223372036854775807.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > What's the maximum number that a long long can hold?
    See limits.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I didn't have a compiler on that computer, but thanks for the suggestion.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM