This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Confusion with a multiplication loop... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion with a multiplication loop..." Watch "Confusion with a multiplication loop..." New topic

Confusion with a multiplication loop...

D diller

Joined: Nov 15, 2007
Posts: 29
So I finally have my program nearly running, but am having some problems with getting my multiplication to work properly:

I think it's a problem with carrying, and it throws my numbers off quite a bit...sometimes. Smaller numbers, where you don't have to carry or only have to carry a few places, it's no problem. But for some reason once you get into carrying quite a bit, it's a huge problem. It's difficult to debug it because it requires going through some LONG multiplication. So I am hoping somebody will be able to spot something in my code that I can't.


(For example: 123*456 yields 41310108 when it should be 56088 - that's a pretty significant 'carrying' error!)
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36482
Do you remember how you were taught long multiplication at the age of 8 at school? That should give you the algorithm to follow.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Your ten's place variable is named "high" so why are you possibly adding it twice. I would rethink this logic:

You are saying if the ten's place is greater than 1 add that value to a certain place in the answer array. Then...always add that value to an additional place in the array? I suspect this isn't what you meant to do.

Lastly, each index inside your array should not have values greater than 10 because each one of them is supposed to be representative of 1 position in a normally represented number. I suspect that when you are adding your ones place and tens place number to answer it goes above 10 and when this happens you are not carrying. So when you add high and low to your array I would check in case this number goes above 9 redo your carry logic.

For example if you have 9 9 9 inside your answer array and you add 1 to the value at index 2 you currently are not checking for values > 9 so it would appear 9 9 10 when you printed it instead of checking.
D diller

Joined: Nov 15, 2007
Posts: 29
Yeah it was something along those lines exactly, just figured it out actually. Thanks for the tips!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36482
Well done
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

hey i am intrested in this solution too

I had once made a division algo using strings, it works so well i could even deploy it as a j2me project
into my mobile phone !!

surely multiplication is an added advantage.

btw a (very very) unoptimal method is "additive multiplication"
eg to calculate 5*3, calculate 5+5+5,

This will not be optimal when it comes to thousands and ten thousands,
But, I dont know why (I have nothing to support my theory),
I think the trade off will not be as much as we expect it to be.

After all, there are middle levels of multiplications involved AND middle levels of additions (with carries) involved for every digit in the multiplier

Just a thought ......

My Website: [] Cool your mind:[] My Sally:[]
I agree. Here's the link:
subject: Confusion with a multiplication loop...
Similar Threads
Can't Find the bug...
Sorting descendingly, but im after ascendingly
u_int8_t in java
am i missing something?
How do I handle "The Matrix" ?