Multiplication can be carried out using repeated addition.  E.g. 6 * 3 = 6 + 6 + 6.  Write a recursive function that takes two arguments as x and y and returns the value of x times y.

 

def multiply_recursive(x, y):

 

    if y == 0:

        return 0  # Base case: any number times 0 is 0

    elif y > 0:

        return x + multiply_recursive(x, y - 1) 

    else:

        return -multiply_recursive(x, -y) 

 

x_val = 6

y_val = 13

result = multiply_recursive(x_val, y_val)

print(f"The result of {x_val} * {y_val} is: {result}")

 

 

The result of 6 * 13 is: 78

 

Go Back