multipication

Python program to Generate multiplication table

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

For loop in Python

While loop in Python

Function in python

Operator in Python

Data type in Python

 

Similar post

Program to Display multiplication table in Java

Program to Display multiplication table in C

Program to Display multiplication table in C++

Program to Display multiplication table in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.