Print multiplication table in Python
In this article, you will learn how to write a Python program to print multiplication table. Such a type of question is generally asked in programming interview.
Python multiplication table using for loop
Here, we have used the for loop to print the multiplication table. First, we have taken the input number from the user. Then we have iterated 10 times using for loop range(1, 11) function. In the initial iteration, the loop iterates and multiplies by 1 the given number. In the second iteration, 2 is multiplied by the given number, and so on.
# Multiplication table in Python
# using for loop
num = int(input("Enter the number : "))
i = 1
# using for loop to iterate multiplication 10 times
print("Multiplication Table : ")
for i in range(1, 11):
print(num,'x',i,'=',num*i)
Output of the above code -
Enter the number : 7
Multiplication Table :
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
Enter the number : 16
Multiplication Table :
16 x 1 = 16
16 x 2 = 32
16 x 3 = 48
16 x 4 = 64
16 x 5 = 80
16 x 6 = 96
16 x 7 = 112
16 x 8 = 128
16 x 9 = 144
16 x 10 = 160
Python multiplication table using while loop
In the given program, we have used the while loop to print the multiplication table in Python. We have declared a variable i and initialized it by 1. Next, we will iterate the while loop until the value of i is smaller and equal to 10. In each iteration, the value of i is incremented by one and multiplied by the num variable. The loop is terminated when the value of i becomes greater than 10.
# Multiplication table in Python
# using while loop
num = int(input("Enter the number : "))
i = 1
# using while loop to iterate multiplication 10 times
print("Multiplication Table : ")
while i<=10:
num = num * 1
print(num,'x',i,'=',num*i)
i += 1
Output of the above code -
Enter the number : 5
Multiplication Table :
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Python Print Multiplication Table from 1 to 10
In the given Python program, we will print the multiplication table from 1 to 10 using the for loop.
# Multiplication table from 1 to 10 in Python
# using for loop
print('Multiplication table from 1 to 10: ')
for x in range (1,11):
print('\n')
for y in range(1, 11 ):
print(x*y, end='\t')
Output of the above code:
Multiplication table from 1 to 10:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Related Articles
Difference between two lists Pythonisalnum function in python
Remove none from list Python
Convert set to list Python
How to give space in Python
Convert list to dictionary Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword