To print the first n multiples of a given number
Expt. No.11
Page No.22
First n multiples
Aim
To write a Python program that prints the first n multiples of a given number, like a multiplication table.
Concepts used
Algorithm
- Input the number and n.
- For i from 1 to n, print number × i.
Program
# First n multiples of a number num = int(input("Enter the number : "))n = int(input("How many multiples? : "))for i in range(1, n + 1): print(num, "x", i, "=", num * i)
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python first_n_multiples.py
Enter the number : 7
How many multiples? : 10
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
✓ Printed by running this program, not typed by hand.
Result
The first 10 multiples of 7 were printed as a table, from 7 x 1 = 7 to 7 x 10 = 70.
Viva voce: what the examiner may ask
01What are the values of i in range(1, n + 1)?
1, 2, … n.
02How would you print the multiples on one line?
print(num * i, end=" ").
03How many times does the loop run?
n times.
Variations for the lab test
- Print the multiples in reverse order.
- Print only the multiples that are even.
- Print tables of 2 to 5 using a nested loop.
! Common mistake Writing
range(n) starts at 0, so the first line printed is 7 x 0 = 0 and the last multiple is missing.