How to Display the Multiplication Table of a Number in Python 🐍


Display the Multiplication Table of a Number in Python

A multiplication table is one of the basic tools used for learning math. It helps visualize the result of multiplying a number by various integers. In this blog, we’ll explore how to display the multiplication table of a number using Python.

By the end of this post, you will learn:

  • How to create a multiplication table for any given number.
  • How to format the table to look clean and easy to read.

What is a Multiplication Table?

A multiplication table displays the product of a number with other numbers, typically from 1 to 10. For example, the multiplication table of 5 looks like this:

[
\begin{aligned}
5 \times 1 & = 5 \
5 \times 2 & = 10 \
5 \times 3 & = 15 \
& \vdots \
5 \times 10 & = 50 \
\end{aligned}
]

The goal is to generate this table for any number entered by the user.


How to Display the Multiplication Table in Python

Let’s write a Python program that will generate and display the multiplication table for any number.


Method 1: Using a For Loop

This is the most common method for generating the multiplication table. We’ll use a simple for loop to multiply the number by values from 1 to 10 and print the result.

Code Example:

# Program to display the multiplication table of a number

# Step 1: Take user input
number = int(input("Enter a number: "))

# Step 2: Use a for loop to display the multiplication table
print(f"Multiplication Table of {number}")
for i in range(1, 11):
    print(f"{number} x {i} = {number * i}")

Explanation of the Code:

  1. User Input:
  • The user is prompted to enter a number, which is stored in the variable number.
  1. For Loop:
  • We use a for loop that iterates from 1 to 10. In each iteration, the number is multiplied by the loop variable i, and the result is printed.
  1. Print the Result:
  • The formatted string f"{number} x {i} = {number * i}" displays each step of the multiplication table.

Sample Output:

Enter a number: 7
Multiplication Table of 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Method 2: Customizing the Range of the Table

We can modify the program to display the multiplication table for any range specified by the user. For example, if you want to see the table from 1 to 20, you can adjust the code accordingly.

Code Example:

# Program to display the multiplication table of a number with custom range

# Step 1: Take user input
number = int(input("Enter a number: "))
start = int(input("Enter the starting range: "))
end = int(input("Enter the ending range: "))

# Step 2: Use a for loop to display the multiplication table within the given range
print(f"Multiplication Table of {number} from {start} to {end}")
for i in range(start, end + 1):
    print(f"{number} x {i} = {number * i}")

Explanation of the Code:

  1. User Input:
  • The user inputs the number, start range, and end range. These values are stored in number, start, and end respectively.
  1. For Loop with Custom Range:
  • The for loop runs from the starting value start to the ending value end. This allows for a customizable multiplication table based on the user’s input.
  1. Print the Result:
  • The table is displayed for the specified range.

Sample Output:

Enter a number: 8
Enter the starting range: 5
Enter the ending range: 15
Multiplication Table of 8 from 5 to 15
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
8 x 11 = 88
8 x 12 = 96
8 x 13 = 104
8 x 14 = 112
8 x 15 = 120

Method 3: Displaying the Table in a Neat Format

Sometimes, the output might look a bit cluttered, especially for larger numbers. We can use Python’s string formatting techniques to ensure that the table looks neat and properly aligned.

Code Example:

# Program to display a neat multiplication table

# Step 1: Take user input
number = int(input("Enter a number: "))

# Step 2: Use a for loop to display the multiplication table in a neat format
print(f"Multiplication Table of {number}")
for i in range(1, 11):
    print(f"{number:2} x {i:2} = {number * i:3}")

Explanation of the Code:

  1. String Formatting:
  • We use :2 and :3 to specify the width of the fields in the print statement, ensuring that the multiplication table looks aligned and easy to read.

Sample Output:

Enter a number: 12
Multiplication Table of 12
12 x  1 =  12
12 x  2 =  24
12 x  3 =  36
12 x  4 =  48
12 x  5 =  60
12 x  6 =  72
12 x  7 =  84
12 x  8 =  96
12 x  9 = 108
12 x 10 = 120

Method 4: Multiplication Table with a Function

To make your program more reusable, you can encapsulate the logic for displaying the multiplication table inside a function. This allows you to easily call the function whenever you need a multiplication table.

Code Example:

# Program to display multiplication table using a function

# Step 1: Define a function to print the multiplication table
def print_multiplication_table(number):
    print(f"Multiplication Table of {number}")
    for i in range(1, 11):
        print(f"{number} x {i} = {number * i}")

# Step 2: Take user input
number = int(input("Enter a number: "))

# Step 3: Call the function to display the multiplication table
print_multiplication_table(number)

Explanation of the Code:

  1. Function Definition:
  • We define a function print_multiplication_table(number) that contains the logic for displaying the multiplication table.
  1. Function Call:
  • After taking user input, we call the function and pass the entered number as an argument to generate the table.

Sample Output:

Enter a number: 9
Multiplication Table of 9
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90

Conclusion

In this blog, we explored multiple ways to display the multiplication table of a number in Python:

  1. Using a for loop for generating the table.
  2. Customizing the range of the table based on user input.
  3. Formatting the table for a neat and aligned display.
  4. Encapsulating the logic in a function for reusability.

Mastering these techniques will help you build solid programming skills. Try these methods and see which one works best for your needs!


Stay tuned for more Python programming tutorials! 😊

Share with our team

Leave a Comment