Multiplication Table in Python

Multiplication tables are a fundamental mathematical concept, used to teach basic arithmetic operations to students. In this article, we will learn how to create a multiplication table in Python. Python is a popular high-level programming language that is widely used for various purposes, including scientific computing, data analysis, and web development.

It is also a great language for beginners because it has a simple syntax and is easy to learn. In this tutorial, we will be learning how to create a multiplication table in Python. We will be using 3 methods to generate the table for any number entered by the user.

There are 3 Methods of Multiplication Tables in Python

Method 1: To print Multiplication Table in Python Using Loop

Python Program to Print Multiplication Table Using a for Loop

n = int(input("Enter any Number  :"));

for i in range(1,11):
 value = n * i
 print(n," * ",i," = ",value)
 

Step 1: Prompt the user to enter a number

We will start by asking the user to input a number for which they want to generate the multiplication table. We will use the ‘input’ function to take the input from the user and the ‘int’ function to convert it to an integer.

Step 2: Create a for loop to generate the multiplication table

Next, we will create a for loop that iterates from 1 to 10. Within the loop, we will multiply the input number by the current iteration number to get the product. We will then print the result using the ‘print’ function.

The ‘range‘ function generates a sequence of numbers from 1 to 10, and we use the ‘i’ variable to iterate over this sequence. We then calculate the product by multiplying the input number ‘n’ with the current iteration value ‘i’, and store it in the ‘value’ variable. Finally, we use the ‘print’ function to display the result.

Step 3: Run the program and test it

Save the program and run it in the Python environment. Enter a number when prompted and the program will generate the multiplication table for that number.

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

Python Program to Print 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  =  130

Method 2: Print the Multiplication Table in Python Using the Function

When using functions 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 a 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.

Python Program to Print Multiplication Table Using Functions

Firstly let’s look at the code:

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

Step 1: Defining the multiply() function

The first step is to define a function that takes two parameters, num and count, and returns the product of the two. Here’s the code for the function:

def multiply(num, count):
    return num * count

This function will be used to calculate the product of the number entered by the user and each number from 1 to 10.

Step 2: Taking user input

The next step is to take input from the user. We will use the input() function to take an integer as input from the user and store it in a variable called n. Here’s the code:

n = int(input("Enter any Number  :"))

Step 3: Printing the multiplication table

The final step is to print the multiplication table of the number entered by the user. We will use a for loop to iterate from 1 to 10 and call the multiply() function to calculate the product of n and the current iteration value. Here’s the code:

for i in range(1, 11):
    print(n, " * ", i, " = ", multiply(n, i))

This code will print the multiplication table of the number entered by the user.

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  =  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 that 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 loops or while loops. It’s the core logic that invokes the function until a condition. There is no definite rule for writing recursive functions. Hence you have to give a lot of focus while writing recursive functions in Python.

The recursive function becomes an infinite loop even with the smallest mistake. Make sure to double-check your code.

Python Program to Print Multiplication Table Using Recursive Function

Recursive multiplication is a function that uses recursion to print the multiplication table of a given number up to 10. Firstly, let’s take a look at the code:

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)

The function rec_multiplication takes two arguments, num and count. num is the number we want to generate the multiplication table for, and the count is a counter that keeps track of how many times we have printed the multiplication table.

Inside the function, we first check if the value of count is less than 11, which means we have not printed the multiplication table up to 10 yet. If count is less than 11, we print the multiplication of num and count using the print() function.

We also increment the value of count by 1 and call the rec_multiplication function recursively with the new value of count. If the value of count is greater than or equal to 11, which means we have printed the multiplication table up to 10, we simply return 1.

Finally, we ask the user to input a number n and call the rec_multiplication function with n as the first argument and 1 as the second argument.

Let’s see an example of how to use this function:

Output
Enter any Number  : 7
7  *  1  =  7
7  *  2  =  14
7  *  3  =  21
7  *  4  =  28
7  *  5  =  35
7  *  6  =  42
7  *  7  =  49
7  *  8  =  56
7  *  9  =  63
7  *  10  =  70

In this example, we input 7 as the number for which we want to generate the multiplication table, and the function prints the multiplication table for 7 up to 10.

That’s it! We have successfully implemented the recursive multiplication function in Python.

Conclusion

In this tutorial, we learned how to create a multiplication table in Python using a simple for loop, functions and recursive function. This is a basic example of how Python can be used to perform calculations and generate useful output. There are many more advanced applications of Python that you can explore as you continue to learn and develop your skills.

And that’s it! You now know how to generate a multiplication table in Python! And lastly Happy Coding!

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.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author