Python Multiplication Table of Number by Function Program

By | March 19, 2020

Python Multiplication Table of Number by Function Program

"<yoastmark

# An example Python program 
# to define and call a function table 
# to show multiplication table of a number 
# Author : www.EasyCodebook.com (c) 
# Note the first line in function is called 
# document string, it 

# define the function with formal parameter num 

def print_table(num): 
    """ This function prints multiplication table of a given number"""
    for i in range(1,11): 
        print(num,' x ', i, ' = ',num*i) 
# end of function table


# input a number
n = int(input("Please Enter a number to print its multiplication table:"))

# call the function tanle by passing actual parameter 'n' 

print_table(n)

Working of Python Multiplication Table Program

First of all we define a user defined function print_table(num). Here num is a formal parameter which is a number to print its table.

print_table(num) function uses a for loop in the range 1 to 10 to display table of a number. Note that the range() function used in for loop uses two argumenst, start =1 and stop=11. It is because the range() function will generate numbers from start to stop-1. Therefore the range() function provides 1 to 10 numbers sequence to for loop. As a result, the multiplication table of the required number is printed from 1 to 10.

After the end of the function, we use an input() function to get input from the user. The user will enter a number. The int() function will convert the entered string into number.

The last statement is a function call statement.

We call the function print_table() by passing it the number.

Now the function print_table() is executed and prints the table.

And this is the end of the Python Multiplication table program using the user defined function print_table(num).

Output

Please Enter a number to print its multiplication table: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

You may also like:

Python Practical Program Examples – Learn By Doing
 
Python program to print table of a number using a user defined table function that takes the number as a parameter and displays multiplication table of that number from 1 to 10.
 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *