Thread: product of even numbers

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

    product of even numbers

    Hello.
    I am trying to create a c program that finds the product of even numbers from 10 to 100. can someone help me? I did the program but the compiler give me the product as 0, but when I try smaller numbers like from 10 to 30 it works. What could be the problem?

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    Post your current code in [code][/code] tags. This is the C++ forum section, there's a separate C section (no pun intended).

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    630
    You want the product of even numbers from 10 to 100. Let's call it from 2 to 100 for simplicity. That's 2 * 4 * 6 * ... * 100 which is 2*(1*2*3*...*50) which is 2 * 50! (50 factorial). 50! is a very very large number, and not counting the product of the even values less than 10 barely makes a dent in it.

    50! = 30414093201713378043612608166064768844377641568960 512000000000000 (65 digits long)

    The product of evens from 10 to 30 is of the order 15! which is 1,307,674,368,000. So that fits in a 64-bit integer.

    Anyway, multiplying 10*12*14*...*100 will wrap around again and again and it only has to become 0 once in the multiplication of factors to make the result 0.
    Last edited by john.c; 10-26-2018 at 09:02 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    My magical senses tell me that you initialized the variable that holds the product to 0. It should be 1, because remember 0 x anything will give you 0. Whereas 1 x anything will give you the number you multiplied with 1. Use that to your advantage!

    And you should be using a for-loop with an iterator initialized to 10 until 100 which increments by 2.

    But like john.c said the actual answer itself would be VERY big, so big that no standard datatype in C++ can store it. So make sure that we're talking about product and not SUM over here. Otherwise you need to use a non-standard datatype that can hold such a value like bigint.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,148
    Quote Originally Posted by Nwb
    My magical senses tell me that you initialized the variable that holds the product to 0. It should be 1, because remember 0 x anything will give you 0.
    That would be a good guess if all of relwan's tests resulted in 0, but "when I try smaller numbers like from 10 to 30 it works", hence relwan could not have initialised that variable to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Oh I totally missed that line laserlight..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Product
    By Cazan Adelin in forum C Programming
    Replies: 6
    Last Post: 04-08-2015, 08:31 AM
  2. Replies: 21
    Last Post: 12-02-2014, 12:13 PM
  3. Matrix product
    By Cotizo in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2008, 10:10 AM
  4. Dot Product.......Help
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-07-2003, 08:49 AM

Tags for this Thread