Output - Python Programming Exercise

This Python program prompts the user for two integer numbers and calculates their product without using the "*" operator. Instead, it uses consecutive additions to simulate multiplication. For instance, multiplying 3 by 5 is achieved by adding 3 five times: 3 + 3 + 3 + 3 + 3 = 15. This approach helps reinforce the concept of repetitive operations and is an excellent exercise for understanding the fundamental mechanics behind multiplication. It also showcases how to implement loops and user inputs in a Python program. This exercise is valuable for beginners looking to enhance their problem-solving skills and grasp the inner workings of mathematical operations in programming. By avoiding the use of the direct multiplication operator, it encourages the use of iteration and conditional logic to achieve a desired result.

 Category

Mastering Flow Control

 Exercise

Output

 Objective

Develop a Python program that prompts the user for two integer numbers and displays their product without using the "*" operator. It should use consecutive additions. (Hint: remember that 3 * 5 = 3 + 3 + 3 + 3 + 3 = 15)

 Example Python Exercise

 Copy Python Code
# Prompt the user to enter the first number
num1 = int(input("Enter the first number: "))

# Prompt the user to enter the second number
num2 = int(input("Enter the second number: "))

# Initialize the product variable
product = 0

# Use a while loop to calculate the product using consecutive additions
i = 0
while i < num2:
    product += num1
    i += 1

# Display the product
print(f"The product of {num1} and {num2} is {product}")

 Output

Enter the first number: 3
Enter the second number: 5
The product of 3 and 5 is 15

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

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.

  •  Absolute Magnitude

    This Python program calculates and displays the absolute value of a number x. The absolute value of a number is defined as the number itself if it is positive,...

  •  Empty Rectangle

    This Python program prompts the user for a symbol, width, and height, and then displays a hollow rectangle using that symbol for the outer border...

  •  Data Analysis

    This Python program calculates various basic statistical operations such as sum, average, minimum, and maximum based on user input. The pr...

  •  Ternary Operator, Positive & Lesser

    This Python program prompts the user to input two numbers and then uses the conditional operator (?) to perform a series of checks on the input. The program first che...

  •  Prime Digit

    This Python program prompts the user to input an integer and determines whether it is a prime number or not. A prime number is defined as a number great...

  •  Dispense Change

    This Python program calculates the change for a purchase, using the largest possible coins or bills. The program prompts the user for the price o...