It depends exactly what your homework question is asking for: if you are supposed to write a
multiply
function which doesn;t use the multiply operator, then there are a couple of ways you could approach this:
1) Brute force and ignorance. x * y is the same as adding x to itself y times:
x * 0 = 0
x * 1 = x
x * 2 = x + x
x * 3 = x + x + x
...
So find the smallest of the two numbers, and loop that many times, adding the other to a total.
2) A bit more finesse. Remember logarithms? log
n(x * y) = log
n(x) + log
n(y)
So you could use the C built in math functions to log
10 x and y, add then and then anti log them to get the result.
3) A lot more finesse! Use bit shift and add in a loop to multiply. An integer is a binary number, so you can treat each bit as a multiplier:
Multiplying and Dividing Binary Numbers | Multiplication and Division[
^] - ANDing a number with 1 tells you if the least significant bit is zero or one, and the two shift operators << and >> multiply and divide by two.
But ... this is your homework, so I'll give you no code!