Function Multiply & MultiplyRecursive - Python Programming Exercise

In this exercise, you will develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The first function should implement an iterative approach, and the second should use recursion. This exercise is perfect for practicing function definition, loops, recursion, and arithmetic operations in Python. By implementing these functions, you will gain hands-on experience in handling function definitions, loops, recursion, and arithmetic operations in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function Multiply & Multiplyrecursive

 Objective

Develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The first function should implement an iterative approach, and the second should use recursion.

 Example Python Exercise

 Copy Python Code
# Function to multiply two numbers using iteration (repeated addition)
def Multiply(x, y):
    # Initialize result to 0
    result = 0
    # Add x to result y times
    for _ in range(abs(y)):
        result += x

    # If y is negative, negate the result
    if y < 0:
        result = -result

    return result

# Recursive function to multiply two numbers using addition
def MultiplyRecursive(x, y):
    # Base case: if y is 0, the result is 0
    if y == 0:
        return 0

    # Recursive case: add x one time and reduce y
    return x + MultiplyRecursive(x, y - 1) if y > 0 else -(x + MultiplyRecursive(x, -y - 1))

# Example usage
x = 5  # First number
y = 3  # Second number

# Call the iterative multiplication function
iterative_result = Multiply(x, y)
print(f"Iterative Multiply({x}, {y}) = {iterative_result}")

# Call the recursive multiplication function
recursive_result = MultiplyRecursive(x, y)
print(f"Recursive Multiply({x}, {y}) = {recursive_result}")

 Output

python multiply.py
Iterative Multiply(5, 3) = 15
Recursive Multiply(5, 3) = 15

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Functions: Hello and Goodbye

    In this exercise, you will develop a Python program where the main function should look like this: SayHello() and SayGoodbye(). You must define the functions SayHello...

  •  Parameterized Function

    In this exercise, you will develop a Python program where the main function should look like this: SayHello("John") and SayGoodbye(). You must define the functions Sa...

  •  Value-Returning Function

    In this exercise, you will develop a Python program where the main function should define and call a sum function that accepts two integers as parameters and returns ...

  •  Value-Returning Function V2

    In this exercise, you will develop a Python program where the main function should look like this: def main(): print("\"Hello, how are you\" contains {0} spaces".form...

  •  Centered Text Function

    In this exercise, you will develop a Python function named "get_int" that displays the text received as a parameter, prompts the user for an integer number, repeats i...

  •  Underline Text Function

    In this exercise, you will develop a Python function that can center the text on the screen (assuming a screen width of 80 characters) and then underline it with hyph...