Display Multiplication Table in Python
What you’ll learn
- How a times table maps to a simple loop in code.
- How to print each row as
base x i = product. - Two Python versions: fixed base and user input, plus live preview.
Overview
Pick a base number and multiply it by 1, 2, 3, ..., 10. A loop prints each row automatically.
Two programs
Fixed table for 5 and user-entered base.
Live preview
Try any integer base and print 1 to 10 rows.
Interview-ready
Simple loop pattern with clear output formatting.
Prerequisites
Python loops, print formatting, and basic input conversion.
- Know
for i in range(1, 11)and multiplication using*. - Optional: convert user input with
int(input())in interactive scripts.
The idea
Fix one base number n. For each counter value i from 1 to 10, print n * i as one row.
The formula is always the same; only i changes each step.
Live preview
Default base is 5 to match example 1. Change it and click Print table.
Algorithm
Goal: print base * i for i = 1..10.
Set base and limit
Choose a base number and how many rows to print (10 here).
Loop from 1 to limit
For each i, compute product = base * i.
Print formatted row
Output as base x i = product.
📜 Pseudocode
procedure print_table(base, last):
print heading
for i from 1 to last:
print base, i, and base * i Table for 5 (fixed base)
Classic 5-times table from 1 to 10.
def print_multiplication_table(base: int, last: int) -> None:
print(f"Multiplication table for {base}:")
for i in range(1, last + 1):
print(f"{base} x {i} = {base * i}")
def main() -> None:
base = 5
last = 10
print_multiplication_table(base, last)
if __name__ == "__main__":
main() Table for a number you type
Reads a positive integer and prints its table up to 10.
def print_multiplication_table(base: int, last: int) -> None:
print(f"Multiplication table for {base}:")
for i in range(1, last + 1):
print(f"{base} x {i} = {base * i}")
def main() -> None:
try:
n = int(input("Enter a positive integer: ").strip())
except ValueError:
print("Could not read an integer.")
return
if n <= 0:
print("Please enter a positive integer.")
return
print_multiplication_table(n, 10)
if __name__ == "__main__":
main() Notes
Formatting. You can align outputs with fixed-width formatting if needed.
Range control. Let users choose last row (10, 12, 20) if assignment asks.
❓ FAQ
🔄 Input / output
Example 1 uses fixed values. Example 2 reads one integer from user input and prints rows 1 through 10.
Edge cases
Non-integer text
Catch conversion errors and show a clear message.
Zero or negative base
Decide whether to allow or reject based on assignment rules.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Print last rows | O(last) | O(1) |
Summary
- Core loop: print
base * ifor each row index. - Cost: linear in number of printed rows.
- Extension: take both base and row limit from user input.
A multiplication table for a number n is the list n x 1, n x 2, .... A loop prints these rows automatically instead of writing each line by hand.
8 people found this page helpful
