Print Multiplication Table of a given Number Using a While Loop in Python

Now we are going to discuss how to print the multiplication table of a given number using a while loop in Python:

Simple code:

num = int(input("Enter a number: "))
i = 1
while i <= 10:
    print(num, "x", i, "=", num*i)
    i += 1

Output:

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Code with comments:

Here is the code with the explanation.

# get the input number from the user
num = int(input("Enter a number: "))

# initialize the counter to 1
i = 1

# continue looping until the counter reaches 10
while i <= 10:
    # print the multiplication table of the input number
    print(num, "x", i, "=", num*i)
    # increment the counter by 1
    i += 1

In the above code, we first get the input number from the user using the input() function and convert it to an integer using the int() function.

We then initialize the counter i to 1 and use a while loop to print the multiplication table of the input number.

At each iteration of the loop, we print the current multiplication table and increment the counter i by 1. Finally, we exit the loop when the counter i reaches 10.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x