Multiplication Table in Python - Code with 3 Different
https://blog.newtum.com/multiplication-table-in-python-code-with-3-different-methods/
Method 1: To print Multiplication Table in Python – Using Loop 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) Output 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 Output 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 = 130Method 2: Print Multiplication table using Function 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 Output 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 = 190Method 3: Using Recursion to Print Mulitplication Table 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) Output 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.
uninstall
DA: 87 PA: 52 MOZ Rank: 26