Python Multiplication Table of Number by Function Program
# 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).
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:
please help me with the following coding, whenever i am doing this multiplication in the end its written as “none”, which i don’t want, please help what should i do i am new to coding
for i in range(1 , 11):
print(n, ” x” , i , “=” ,n*i)
p = multiplication(6)
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Just call the function as
p = multiplication(6)
because this function does not return a value
but just prints the table. so we will not use variable on left side
of assignment. Actually there is no need to call this function in assignment statement.