Python Program To Generate Multiplication table

Problem Definition

Create a Python Program to generate the multiplication table of a number.

Program

num = int(input("Enter the Number :"))

for i in range(1,11):
    print("{} x {} = {}".format(num,i,num*i))

Output

Enter the Number :3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Explanation

First, we are taking input from the keyboard using the user using the input() method then later we are computing the table using a for loop in range of 1 to 11 then we are simply printing out the result using string formatting.

Alternatively, if you are using Python 3.6 or above you can take advantage of f-strings for string formatting.

Program

num = int(input("Enter the Number :"))

for i in range(1,11):
    print(f"{num} x {i} = {num*i}")

Output

Enter the Number :3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Using While Loop

num = int(input("Enter the Number :"))

i = 1
while(i<=10):
    print("{} x {} = {}".format(num,i,num*i))
    i += 1

Output

Enter the Number :4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

To learn more about While loops in Python reading the following articles is recommended.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close