Sai A Sai A
Updated date Nov 26, 2023
In this blog, we will explore how to print multiplication tables in Python. Explore three distinct methods, from traditional loops to short list comprehensions.

Introduction:

Understanding multiplication tables is a cornerstone of mathematical proficiency, and translating this knowledge into a Python program can be an enlightening experience. In this blog, we will learn how to create a Python program that generates multiplication tables. This not only reinforces your coding skills but also provides a hands-on opportunity to explore the intersection of mathematics and programming.

Method 1: Using a For Loop

We use a for loop to iterate from 1 to 10, calculating the product of the given number and the current iterator.

# Method 1: Using a for loop
def multiplication_table_for(num):
    print(f"Multiplication Table for {num} using a for loop:")
    for i in range(1, 11):
        result = num * i
        print(f"{num} x {i} = {result}")

# User input
number_for = int(input("Enter the number for the multiplication table (Method 1): "))

# Output
multiplication_table_for(number_for)

Output:

Enter the number for multiplication table (Method 1): 4
Multiplication Table for 4 using a for loop:
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

Method 2: Using a While Loop

Similar to Method 1, but with a while loop. A counter variable is initialized outside the loop and incremented until it reaches 10.

# Method 2: Using a while loop
def multiplication_table_while(num):
    print(f"\nMultiplication Table for {num} using a while loop:")
    i = 1
    while i <= 10:
        result = num * i
        print(f"{num} x {i} = {result}")
        i += 1

# User input
number_while = int(input("\nEnter the number for the multiplication table (Method 2): "))

# Output
multiplication_table_while(number_while)

Output:

Enter the number for multiplication table (Method 2): 6

Multiplication Table for 6 using a while loop:
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Method 3: Using List Comprehension

List comprehensions offer a concise way to create lists in Python. Here, we generate a list of strings representing each multiplication statement.

# Method 3: Using list comprehension
def multiplication_table_list_comprehension(num):
    print(f"\nMultiplication Table for {num} using list comprehension:")
    table = [f"{num} x {i} = {num*i}" for i in range(1, 11)]
    print('\n'.join(table))

# User input
number_list_comp = int(input("\nEnter the number for the multiplication table (Method 3): "))

# Output
multiplication_table_list_comprehension(number_list_comp)

Output:

Enter the number for multiplication table (Method 3): 9

Multiplication Table for 9 using list comprehension:
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 have discussed various methods for generating multiplication tables in Python. Each method—whether using a for loop, a while loop, or list comprehension—offers a unique approach to achieve the same result.

Comments (0)

There are no comments. Be the first to comment!!!