4 December 2010

Multiplication without the multiplication operator (*)

I was asked 2 days ago how to you create a multiplication function without using the multiplication operator?

Eesh, a bit of head scratching and then a while after these were the possibilities
  1. Use a while loop to iterate over one of the values
  2. Use a for loop to iterate over one over the values
  3. Use recursion to call yourself until a drop-out condition occurs
Other things to think about
  1. Negative numbers. If one number is negative then the returning value will be negative. If both are negative or both positive then return positive
  2. Iterate over the smallest number to be more efficient

No comments: