How to Create a Multiplication Table in Python Using a For Loop?

Estimated read time 1 min read

Here’s an example of how to create a multiplication table in Python using a for loop:

def multiplication_table(n):
    for i in range(1, n+1):
        row = []
        for j in range(1, n+1):
            row.append(i*j)
        print(row)

# 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 a for loop to iterate over the numbers from 1 to n and creates a list row that contains the products of each number with the numbers from 1 to n. The row list is then printed as a row in the multiplication 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