Statement
Solution
Exercise 3.1: Multiplication Table
Loops allow us to repeat actions multiple times. Create a program that prints the multiplication table for a number entered by the user.
Your program should:
- Ask the user to enter a number (integer).
- Use a For Loop to iterate from 1 to 10.
- In each iteration, calculate the product (number * iteration).
- Print the result in the format:
number x i = result.
Sample Interaction:
Input
Output
7
Enter a number: 7
--------------------
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70
Solution
We use the range() function to generate numbers from 1 to 10. Note: range(1, 11) stops before 11.
# 1. Get User Input
num = int(input("Enter a number: "))
print("-" * 20)
# 2. Loop from 1 to 10
# range(start, stop) includes start, excludes stop.
for i in range(1, 11):
result = num * i
print(f"{num} x {i} = {result}")
Key Concepts:
for i in range():creates a loop that runs a specific number of times.range(1, 11)generates the sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.- The variable
iupdates automatically in every step of the loop.
Test Console
Run code to see output...