In examinations or Interviews, or in 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.
What is Multiplication Table?
A multiplication table is the list of multiples of a particular number in 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, and 5 and the multiplication table of 10 are the easiest one. 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.
Multiplication Table in Python Using 3 Different Methods
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 using for loop. 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 the range function, which directs the loop to start from 1 and run till less than 11 i.e 10.
Inside the for loop, multiply “i” by “n” and store 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 used to write in the book during our School Days.
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 programs, the architect is the biggest challenge. Architect in the sense of what part should go in function. Ideally, the 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 Multiplication Table in Python, multiplication operations will be performed again and again. So we will write the “function multiply” with two input parameters. And the return parameter will be the result, just like the 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: Print Multiplication Table Using Recursion
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 for writing recursive functions. Hence you have to give lot of focus while writing recursive function in python.
The recursive function becomes an infinite loop even with the smallest mistake. Make sure to double-check your code.
Program 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 no loops. Just a condition inside a function. If the condition is true, the function calls it itself. If you mess with the condition, your function will keep on running till infinity.
Conclusion and Summary
A multiplication table in python is an important example of python. It helps to solve the problem of printing complex multiplication tables of numbers that are 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 the multiplication table is something every student learn, tutors can focus on programming concept.
The biggest catch for tutors and students is to find examples that students understand at the functional level but not at the programming level. This helps to focus on the technicalities of programming very, very easily. Make sure you study maths very well if you want to become a great developer in future.
If you want to learn python programming, you can refer to this Python Online Course with Certification. It’s easy to understand and meant for beginners who have no background in programming.