In this post, we will write a Python program to display the multiplication table. We’ll use a while-loop, a for-loop, and a function, providing detailed explanations and examples for each approach.
So without any further ado, let’s get started…
Python Program to Display the Multiplication Table Using while-loop
Let’s start with the algorithm. We will discuss each and every step to write this program and then jump into source code.
Algorithm
- Take input from the user (
num
). - initialize
i = 1
. while i<=num
doprint(f"{num} X {i} = {num*i}")
i+=1- End the program
Based on the above algorithm, we now understand how to write this program but Before we delve into the programming part, there are a few essential programming concepts you should be familiar with:
Source Code
num = int(input("Enter a number: "))
i = 1 # initialization
while i<=10:
print(f"{num} X {i} = {num*i}")
i+=1
Output
Enter a number: 5 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 Program to Display the Multiplication Table Using For-loop
Algorithm to write program to display multiplication table using for-loop is very similar to while-loop algorithm there is only syntax changed so let’s seen it.
Algorithm
- Take input from the user (
num
). - Using for-loop iterate from 1 to 10 (
for i in range(1,11)
). - Inside for-loop
print(f"{num} X {i} = {num*i}")
- End the program
From the above algorithm, the steps are clear but Before we jump into the programming part few programming concepts you have to know:
Source Code
num = int(input("Enter a number: "))
for i in range(1,11):
print(f"{num} X {i} = {num*i}")
Output
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 |
Python Program to Display the Multiplication Table Using Function
Algorithm
- define a function with the name mul_table
(def mul_table(n)
). - Inside the function do:
for i in range(1,11):
print(f"{num} X {i} = {num*i}")
- Outside of the function take input from the user (
num
). - at the end call the function with the user input (
mul_table(num)
).
From the above algorithm, the steps are clear but Before we jump into the programming part few programming concepts you have to know:
Source Code
def mul_table(n): # define a function
for i in range(1,11):
print(f"{num} X {i} = {num*i}")
num = int(input("Enter a number: "))
mul_table(num) # calling a function
Output
Enter a number: 9 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 9 X 10 = 90 |
This covers how we can write a Python program to display the multiplication table using different approaches. I hope you understand all the approaches, and thank you for reading this article.