Table of Contents
Python program to Generate multiplication table
In this article, we will discuss the python program to Generate multiplication table
In this program, we are going to learn how to generate a multiplication table using 3 ways in Python language.
This is done using for loop , while loop ,and function
Code to Generate multiplication table
Code to Generate multiplication table – using for loop
In this program, we will display multiplication table of given number using for loop in Python language.
Program 1
num=int(input("Enter an integer")) #Take input from the user #iterate to times from i 1 - 10 for i in range(1,11): print(num,'X', i,' = ',num*i)
When the above code is executed , it produces the following result
Enter an integer 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
- integer variable num are declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using input stream and store to the variable num
- Create a for loop of i from 1 to 10 and increase the value of i after every iteration by 1 using range() function
- finally, the program displays the multiplication table using print() function.
Code to Generate multiplication table – using while loop
In this program, we will display multiplication table of given number using while loop in python language.
Program 2
num=int(input("Enter an integer")) i=1 while i<=12: print(num,'X', i,' = ',num*i) i=i+1
When the above code is executed , it produces the following result
Enter an integer 13 13 * 1 = 13 13 * 2 = 26 13 * 3 = 39 13 * 4 = 52 13 * 5 = 65 13 * 6 = 78 13 * 7 = 91 13 * 8 = 104 13 * 9 = 127 13 * 10 = 130
- integer variable num are declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using input stream and store to the variable num
- Create a while loop of i from 1 to 10 and increase the value of i after every iteration by 1 using range() function
- finally, the program displays the multiplication table using print() function.
Code to Generate multiplication table – using function
In this program, we will display multiplication table of given number using function in Python language
Program 3
#Program to Generate multiplication table - using function def multiTable(num): #function definition for i in range(1,11): print(num,'X', i,' = ',num*i) #input a number num=int(input("Enter an integer")) #call the function multiTable(num)
When the above code is executed , it produces the following result
Enter an integer15 15 X 1 = 15 15 X 2 = 30 15 X 3 = 45 15 X 4 = 60 15 X 5 = 75 15 X 6 = 90 15 X 7 = 105 15 X 8 = 120 15 X 9 = 135 15 X 10 = 150
Suggested post
Similar post
Program to Display multiplication table in Java
Program to Display multiplication table in C