Skip to main content

Python Program to Create Mathematical Table of an Integer

Python Program to Create Mathematical Table


Multiplication table in mathematics is a list of multiples of a number from 1 to 10. According to Wikipedia, the Multiplication table is a mathematical table used to define a multiplication operation for an algebraic system.

Procedure 

Initially, the number whose multiplication table is to be displayed is received as input. 

Then a list of multiples(up to 10) of the number is created by calling the function multiTable(num), with the received input as argument. Suppose the received input from the user is 7. In that case, the following list is created.
prodList = [1, 14, 21, 28, 35, 42, 49, 56, 63, 70 ]

Afterward, a counter(index) is added to the iterable(here prodList) using enumerate() method; which in turn returns as enumerate object. Finally, the multiplication table is printed based on the counter and the iterable.

Source Code

Source Code


prodList = []
def multiTable(num):
    """"
    Creates a list of multiples of a number received as input 
    """
    global prodList
    for i in range(1, 11):
        prod = num * i
        prodList.append(prod)
  
num = int(input('Enter the number whose multiplication table is to be displayed: '))
multiTable(num)
print(prodList)
print('\n')
print('The multiplication table for {0} is:' .format(num))
for index, ele in enumerate(prodList, 1): # Creates multiplication table of a number
    print('{0} x {1} = {2}' .format(index, num, ele))

Output

Enter the number whose multiplication table is to be displayed: 70
[70, 140, 210, 280, 350, 420, 490, 560, 630, 700]


The multiplication table for 70 is:
1 x 70 = 70
2 x 70 = 140
3 x 70 = 210
4 x 70 = 280
5 x 70 = 350
6 x 70 = 420
7 x 70 = 490
8 x 70 = 560
9 x 70 = 630
10 x 70 = 700

Update:

The program can be optimised by using list comprehension to create a list of multiples of the number received as user input. Consequently, the function multiTable() can be eliminated. 

The updated source code:
Updated Source Code

import time

try:
    start = time.time()
    prodList = []
    num = int(input('Enter the number whose multiplication table is to be displayed: '))
    prodList = [num * i for i in range(1, 13)] #list comprehension to create a list of multiples of number received as input
    print(prodList)
    aster = '*'
    print(aster * 80)
    
    print('The multiplication table for {0} is:' .format(num))
    for index, ele in enumerate(prodList, 1): #Prints multiplication table of the number
        print('{0} x {1} = {2}' .format(index, num, ele))
        
    print(aster * 80)
    print ('Time to run program: ' +str(time.time()-start)) 
    
except(ValueError):
    print('Please enter a valid integter number')


Watch this space for more updates. Happy Coding!!!

Links

https://github.com/pbipin/Multiplication-Table-in-Python-3





Comments

Popular posts from this blog

Python Program to Convert Floating Point Decimal to Binary

Decimal vs Binary The decimal numeral system in which numbers are expressed in scales of 10. Whereas the binary numeral system involves expressing numbers in scales of 2. Procedure Step 1: The whole number part(59) of 59.65625 is divided by 2 until the quotient is 1. The remainder(carry) that is accumulated(either 1 or 0) till the last sequence of operation that gives the quotient 1 is taken in the reverse order of creation, so as to obtain 111011.  59/2 = 29 (1 is taken as carry)  29/2 = 14 (1 is taken as carry)  14/2 = 7 (0 is taken as carry)  7/2 = 3 (1 is taken as carry)  3/2 = 1 (1 is taken as carry)  So when we reverse the carry, we get 11011. Finally, we place the final quotient at the very beginning of the 11011 to get 111011. Step 2: The decimal part is multiplied by 2 as many times as the number of decimal places in the final result required. To elaborate, let's take the above mentioned example of 59.65625. Here 0.65625...

Python Program to Convert Decimal to Binary

The decimal numeral system in which numbers are expressed in scales of 10. Whereas the binary numeral system involves expressing numbers in scales of 2. Procedure: Suppose we want to convert decimal number 59 to binary. The number 59 is divided by 2 until the quotient is 1. The remainder(carry) that is accumulated(either 1 or 0) till the last sequence of operation that gives the quotient 1, is taken in the reverse order of creation so as to obtain 111011.  59/2 = 29 (1 is taken as carry)  29/2 = 14 (1 is taken as carry)  14/2 = 7 (1 is taken as carry)  7/2 = 3 (1 is taken as carry)  3/2 = 1 (1 is taken as carry)  So when we reverse the carry, we get 11011. Finally, we place the final quotient at the very beginning of the 11011 to get 111011. Source Code: def dec2bin(num):     """"     Function to convert a decimal number to binary number     """     global wholeList   ...