Python Program To Generate Multiplication table

2 min read

Problem Definition

Create a Python Program to generate the multiplication table of a number.

Program

num = int(input("Enter the Number :"))

for i in range(1,11):
    print("{} x {} = {}".format(num,i,num*i))

Output

Enter the Number :3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Explanation

First, we are taking input from the keyboard using the user using the input() method then later we are computing the table using a for loop in range of 1 to 11 then we are simply printing out the result using string formatting.

Alternatively, if you are using Python 3.6 or above you can take advantage of f-strings for string formatting.

Program

num = int(input("Enter the Number :"))

for i in range(1,11):
    print(f"{num} x {i} = {num*i}")

Output

Enter the Number :3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Using While Loop

num = int(input("Enter the Number :"))

i = 1
while(i<=10):
    print("{} x {} = {}".format(num,i,num*i))
    i += 1

Output

Enter the Number :4
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

To learn more about While loops in Python reading the following articles is recommended.


PROGRAMS

Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read