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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.