Zed-King Institute

    Python Program to Print a Multiplication Table

    easyfor loopPython

    The first loop program most students write. It shows how range() produces the numbers a loop walks through.

    The question

    Read an integer n and print its multiplication table from 1 to 10 in exactly this format: n x i = product (one line each).

    The code

    program.py

    n = int(input())
    for i in range(1, 11):
        print(n, "x", i, "=", n * i)

    Input

    14

    Output

    14 x 1 = 14
    14 x 2 = 28
    14 x 3 = 42
    14 x 4 = 56
    14 x 5 = 70
    14 x 6 = 84
    14 x 7 = 98
    14 x 8 = 112
    14 x 9 = 126
    14 x 10 = 140

    How it works

    1. 1range(1, 11) gives 1, 2 … 10 — the end value 11 is not included.
    2. 2For each i, n * i is the next line of the table.
    3. 3print(n, "x", i, "=", n * i) puts one space between each item automatically.

    Common mistakes

    • Writing range(1, 10), which stops at 9 and misses the last line.
    • Joining numbers and text with + — Python cannot add a number to a string without str().

    Multiplication table using a while loop

    A while loop needs three things that range() did for you: start the counter (i = 1), test it (i <= 10) and increase it (i += 1).

    program.py

    n = int(input())
    i = 1
    while i <= 10:
        print(n, "x", i, "=", n * i)
        i += 1

    Input

    7

    Output

    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

    Table on a single line, separated by spaces

    end=" " replaces the new line that print() normally adds, so all ten answers stay on one line. The last print() ends that line.

    program.py

    n = int(input())
    for i in range(1, 11):
        print(n * i, end=" ")
    print()

    Input

    7

    Output

    7 14 21 28 35 42 49 56 63 70

    Questions students ask

    How do I print a multiplication table from 1 to 10 in Python?
    Loop with for i in range(1, 11) and print n * i on each pass. range(1, 11) stops at 10 because the end value is not included.
    How do I print the table up to 20 instead of 10?
    Change range(1, 11) to range(1, 21). Nothing else in the program changes.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    Preparing for NIELIT O Level?

    This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.

    More in for loop