How to Create a Multiplication Table in Python?

Estimated read time 1 min read

A multiplication table is a table showing the products of two numbers. Here’s an example of how to create a multiplication table in Python:

def multiplication_table(n):
    for i in range(1, n+1):
        for j in range(1, n+1):
            print("{:3d}".format(i*j), end=" ")
        print()

# test the function with a sample input
multiplication_table(10)

In this solution, we define a function multiplication_table that takes an integer n as input. The function uses two nested loops to iterate over the numbers from 1 to n and prints the product of each pair of numbers. The {:3d} format string is used to format the output with a width of 3 digits, which aligns the columns in the table.

In the main part of the script, we test the function with a sample input n of 10 and call the multiplication_table function to create the multiplication table.

To use this code with other values of n, simply replace n with the desired value.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply