A multiplication table displays the products of numbers from 1 to N. Here N is the size of the table we will take N = no of the tables we want.
-
For single number table
First, let’s define a Python function called “multiplication_table” that takes the size of the table (‘number’) as input. Here for example we will take the number as 5.
Python code looks like
def multiplication_table(number): for i in range(1, 11): print(f"{number} x {i} = {number * i}") multiplication_table(5)
The third line prints out the multiplication table entry for the current value of i. It uses f-strings to insert the values of, number, i and the product of number, i into the string. Then run the program by writing its function name.
Showing Output
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
-
For multiple numbers tables
Step 1: Define the Multiplication Table Function
First, let’s define a Python function called “multiplication_table” that takes the size of the table (‘n’) as input.
We will take the range to (n+1), showing the term till ‘n‘. Then the ” end=”\t” ” is used as the terminating argument.
def multiplication_table(n): for i in range(1, n+1): for j in range(1, n+1): print(i * j, end="\t") print()
Your code doesn’t end here.
Step 2: Generate the Multiplication Table
Next, we’ll call the “multiplication_table” function with the desired size of the table as an argument. For example, to generate a 10×10 multiplication table, we’ll call the function n=10 ( you can choose any number you desire).
multiplication_table(10)
now your code ends here.
In the end, your Python code looks like this.
def multiplication_table(n): for i in range(1, n+1): for j in range(1, n+1): print(i * j, end="\t") print() multiplication_table(10)
Showing Output
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100