Hi,
Just started Java and want to multiply odd numbers between 1 and 100.
However, at the moment i can only get them to add. I tried replacing
the + sign with the * multiplication sign but to no avail. Any
suggestions?
int sum = 0;
for ( int number = 0; number <=100; number +=2 )
sum += number;
JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Sum Odd
Integerts from 1 to 100", JOptionPane.INFORMATION_MESSAGE );
Monique Y. Mudama - 31 Jan 2006 20:50 GMT
> Hi,
> Just started Java and want to multiply odd numbers between 1 and 100.
[quoted text clipped - 9 lines]
> JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Sum Odd
> Integerts from 1 to 100", JOptionPane.INFORMATION_MESSAGE );
Yup. Walk through the loop.
Your loop starts with number = 0.
Then you multiply sum by 0, resulting in sum = 0.
And then you keep multiplying 0 by stuff, all of which of course
results in 0.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Daniel Dyer - 31 Jan 2006 20:50 GMT
> Hi,
> Just started Java and want to multiply odd numbers between 1 and 100.
[quoted text clipped - 9 lines]
> JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Sum Odd
> Integerts from 1 to 100", JOptionPane.INFORMATION_MESSAGE );
Your range is 0-99, not 1-100. What happens when you multiply something
by zero?
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Daniel Dyer - 31 Jan 2006 20:51 GMT
>> int sum = 0;
>>
[quoted text clipped - 8 lines]
>
> Dan.
Sorry your range is 0-100, but the point still stands. Initialise number
to 1 instead of 0 and it should work.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
shannon - 31 Jan 2006 21:33 GMT
Thanks, should have realised that.
Replaced number=0 with number=1 and i still get an answer of 0. I
still need to be able multiple them which I can't get working
Thanks for helping me out with this.
Daniel Dyer - 31 Jan 2006 21:49 GMT
> Thanks, should have realised that.
> Replaced number=0 with number=1 and i still get an answer of 0. I
> still need to be able multiple them which I can't get working
> Thanks for helping me out with this.
I think Roedy identified the problem. It's going to be a very big number,
bigger than can be represented even in a long, let alone an int. You will
get overflow, so the answer will not be correct.
The biggest factorial that can be represented in a 64-bit signed integer,
such as a Java long, is 20! (20 x 19 x 18 x 17 ... x 2 x 1). The highest
positive number you can store is 2^63 or about 9.22 x 10^18 (9.2 billion
billion). Your number is much bigger. If you want to calculate that you
will have to look into the BigInteger class.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
shannon - 31 Jan 2006 21:58 GMT
I have tired multiplying numbers from 1 to 20 but that doesn't work for
me, How can i get it to work as it will suffice.
Daniel Dyer - 31 Jan 2006 22:05 GMT
> I have tired multiplying numbers from 1 to 20 but that doesn't work for
> me, How can i get it to work as it will suffice.
What code do you have now? Did you change the type of sum from int to
long? You will only be able to do 12! with an int.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
shannon - 31 Jan 2006 22:33 GMT
Thanks for the help all, got it working.
Roedy Green - 31 Jan 2006 21:30 GMT
> int sum = 0;
>
> for ( int number = 0; number <=100; number +=2 )
> sum += number;
you mean this/
Try it with 9 and see if your code works. You may be pleasantly
surprised. A repeating multiplication is called a sum not a product.
What you are doing is very similar to computing a factorial and runs
into the same problem with the answers being too big.
see .http://mindprod.com/jgloss/factorial.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Eric Sosman - 31 Jan 2006 22:52 GMT
shannon wrote On 01/31/06 15:38,:
> Hi,
> Just started Java and want to multiply odd numbers between 1 and 100.
[quoted text clipped - 9 lines]
> JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Sum Odd
> Integerts from 1 to 100", JOptionPane.INFORMATION_MESSAGE );
Your difficulty is related to an old schoolboy
algebra problem: As quickly as you can, multiply the
twenty-six factors
(x-a) * (x-b) * (x-c) * ... * (x-z)
and write down all the terms. Hint: The result is
not a 26th-degree polynomial.

Signature
Eric.Sosman@sun.com
Mike - 03 Feb 2006 06:15 GMT
> Your difficulty is related to an old schoolboy
> algebra problem: As quickly as you can, multiply the
[quoted text clipped - 4 lines]
> and write down all the terms. Hint: The result is
> not a 26th-degree polynomial.
OK, I give up. What is the real answer?
Thomas Hawtin - 03 Feb 2006 06:28 GMT
>> Your difficulty is related to an old schoolboy
>> algebra problem: As quickly as you can, multiply the
[quoted text clipped - 6 lines]
>
> OK, I give up. What is the real answer?
Hint: Look at the third from last factor.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 03 Feb 2006 07:19 GMT
On Fri, 03 Feb 2006 06:46:21 +0000, Thomas Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :
>Hint: Look at the third from last factor
(x - x )

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Mike - 04 Feb 2006 03:51 GMT
> >> Your difficulty is related to an old schoolboy
> >> algebra problem: As quickly as you can, multiply the
[quoted text clipped - 8 lines]
>
> Hint: Look at the third from last factor.
Aaarghh!! Caught me!. Thanks. :)