Q.Write a function to print the table of a given number. The number has to be entered by the user.
Define print_table(num) that loops i through range(1, 11) and prints one line "num x i = num*i" per pass — a fixed 10-times repetition, the textbook use case for a for loop with range().
The idea. A multiplication table is the same computation done 10 times with only the multiplier changing — a counted repetition, so a for loop over range(1, 11) is the right tool (recall stop is excluded, hence 11). Packaging it as a function makes the table reusable: the user-input part stays outside, the logic inside.
Program
def print_table(num):
# prints the multiplication table of num from 1 to 10
for i in range(1, 11):
print(num, "x", i, "=", num * i)
n = int(input("Enter a number: "))
print_table(n)
Sample run
Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Key lines. int(input(...)) converts the typed string to a number; print_table(n) passes it as the parameter num; inside, i takes values 1, 2, …, 10 and print composes each line (print's commas insert single spaces automatically).
To print a table up to any limit, add a second parameter with a default: def print_table(num, upto=10): and loop over range(1, upto + 1).
The function loops i over range(1, 11), printing num x i = num*i each time; for the user input 7 it outputs the complete table from 7 x 1 = 7 to 7 x 10 = 70.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.