Python Program to Print Table of a Given Number














































Python Program to Print Table of a Given Number



Hello, this article is about a python program to generate the multiplication table of a given number. The number whose table is to generated is going to be given by the user.

We will use input() function in python to take input from the user. This function by default takes input as a string (collection of alphabets/characters). So to make it as an integer number we have to typecast it. So to make it integer we use the int() function in these are predefined functions present in python.

Example: 

1
number = int(input("Enter the number: "))

Now after taking the number as input we are going to iteratively multiply this number with 1,2,3......upto 10.
So whenever something iterative process comes up we have to use loops in python. So we are using for loop here.
with for loop, we used a function named range() which takes 2 arguments one is the value from which the loop will start to execute and second is the value up to which it will run but it stops just before that exact value.

For example, if we want to run it from 1 to 10 we have to write as range(1,11).  Which implies it starts from 1 but end just before 11 that is 10.

So in the loop, we multiplied the number given by the user first with 1 then 2 and so on iteratively until termination of the loop or in easy terms ending of the loop. and we printed the value using formatting in python.

The complete Source Code:
1
2
3
4
5
#Python Code To Generate Table of a given number !!
number = int(input("Enter the number: "))
print("The table of %d is"%(number)) 
for i in range(1,11):
    print("%d X %d = %d" % (number,i,number*i)) # printing the table

OUTPUT:
Enter the number: 5                                                                                                                                                          
The table of 5 is                                                                                                                                                            
5 X 1 = 5                                                                                                                                                                    
5 X 2 = 10                                                                                                                                                                   
5 X 3 = 15                                                                                                                                                                   
5 X 4 = 20                                                                                                                                                                   
5 X 5 = 25                                                                                                                                                                   
5 X 6 = 30                                                                                                                                                                   
5 X 7 = 35                                                                                                                                                                   
5 X 8 = 40                                                                                                                                                                   
5 X 9 = 45                                                                                                                                                                   
5 X 10 = 50   

Lastly, if you see while printing we used something called formatting in python. This %d refers to integer values if we want to print any integer then it is referred as %d and after the % marks you see a tuple containing the 3 variables first the input number, the loop counter with which number is getting multiplied and lastly the result of the multiplication. If you are coming from a language background like C programming then this might seem familier to you.

More Articles of Subham Banerjee:

Name Views Likes
Python Program to Print Table of a Given Number 338 0

Comments