Examination or Interview or Real Life we need the Mathematics of Multiplication. And hence this session focus on the program for multiplication table in Python.
If you want to see all the practice examples and Explanations of Python then please use this refer this URL. Click over here for the complete Python Practice Series.
So Exactly What is Multiplication Table ?
“Hey man, I know this I want the code!!”.
I know you are having this irritating statement in your mind. But here I want to give just a detail to my readers who don’t know it about Multiplication table. And I also explain to explain why we need to write the program of Multiplication table in Python.
A multiplication table is the list of multiples of a particular number in the sequential order typically from 1 to 10.
And the fun fact is there is no formal education without the multiplication table. And in school days we have written multiplication tables of 2 4, 5 and the multiplication table of 10 is the easiest ones.
But just imagine when you have to write the multiplication tables of 13 or 37 or 169
Then the nightmare begins. No one numbers remember these multiple tables. There is no other option other than having a calculator or doing all the calculations manually.
And believe me, no one has that much time especially in the era of computers and online.
So to solve this problem we can write a simple program, to accept the input from the user and print the multiplication table of that program.
Method 1: To print Multiplication Table in Python – Using Loop
Program to write Multiplication table using For Loop
n = int(input("Enter any Number :")); for i in range(1,11): value = n * i print(n," * ",i," = ",value)
Enter any Number :5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
The above program is a simple program to print multiplication tables in Python.
Here, the user enters the input of any number of his choice, but since we have used “int” it cannot accept the decimal values.
Then we write the “for loop” which uses “i” as a counter variable. Then we have used range function which directs the loop to start from 1 and run till less than 11 i.e 10.
Inside the for loop, mulitply “i” by “n” and stored in the result in the variable value.
And the next print statement is very important to display the result properly. Just like the multiplication table we use to write in the book during our School Days.
If you want the video explanation of the Program , don’t worry we got you covered. Please go through below video all your queries will be resolved.
We can even write multiplication table in python using while loop like below.
Program to write Multiplication table using While Loop
n = int(input("Enter any Number :")); i = 1 while i < 11: value = n * i print(n," * ",i," = ",value) i = i + 1
Enter any Number :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 = 117 13 * 10 = 130
Method 2: Print Multiplication table using Function
When using function in any of the python program , architect is the biggest challenge. Architect in the sense what part should go in function.
Ideally function should include the same type of code which is executed again and again. This way we reduce the compiling time and increase the coding lines.
In the example of Mulitplication Table in Python , multiplication operation will be performed again and again. So we will write the “function mulitply” with two input parameters. And the return parameter will be the result. Just like below code
Program to print Multiplication table using Functions
def multiply(num,count): return num * count n = int(input("Enter any Number :")); i = 1 for i in range(1,11): print(n," * ",i," = ",multiply(n,i)) i = i + 1
Enter any Number :19 19 * 1 = 19 19 * 2 = 38 19 * 3 = 57 19 * 4 = 76 19 * 5 = 95 19 * 6 = 114 19 * 7 = 133 19 * 8 = 152 19 * 9 = 171 19 * 10 = 190
Method 3: Using Recursion to Print Mulitplication Table
Hold your breath and focus. Recursion is the most challenging part even for the senior developer today.
Recursion means calling again and again. So, here we will write a function which will keep on itself until the condition is met i.e till the counter reaches 10.
In recursion we don’t use loops like for loop or while loop. It’s the core logic that invokes the function until a condition. There is not definite rule to write recursive function. Hence your have to give lot of focus while writing recursive function in python.
Recursive function become an infinte loop even with a smallest mistake. Make sure to double check your code.
Code to print Multiplication table using Recursive Function
def rec_multiplication(num,count): if( count < 11): print(num," * ",count," = ",num * count) return rec_multiplication(num,count + 1) else: return 1 n = int(input("Enter any Number :")); rec_multiplication(n,1)
Enter any Number :67 67 * 1 = 67 67 * 2 = 134 67 * 3 = 201 67 * 4 = 268 67 * 5 = 335 67 * 6 = 402 67 * 7 = 469 67 * 8 = 536 67 * 9 = 603 67 * 10 = 670 >
Look at the code above. You will see there are is no loops. Just a condition isndie a function. If the condition is true the function calls it itself. You mess with condition , you function will keep on running till infinity.
Conclusion and Summary
Mulitplication table in python is an important example in python. It helps to solve the problem of printing complex mutlipclaition table of the numbers that’s not possible to remember like 167, 193 etc.
But the important reason behind learning this example is totally different. It helps you to understand the complex system of loops , functions and recursion. Since multiplication table is something every students know , tutors can focus on programming concept.
The biggest catch for tutors and students is to find the examples that student understand at functional level but not at programming level. This helps to focus on technicalities of programming very very easily.
Make sure you study maths very well if you want to become a great developer in future.
If yo want to learn python programming you can refer this Python online Course with Certification. It’s easy to understand and meant for beginners who have no background in programming.