close

How do I use while loops to create a multiplication table in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I use while loops to create a multiplication table in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I use while loops to create a multiplication table in python?

  1. How do I use while loops to create a multiplication table in python?

    The reason why you have an infinite loop on your hands is because you are comparing i to num, while also increasing num on every run. If you make sure i is always <= 10, you get your desired output:

  2. I use while loops to create a multiplication table in python

    The reason why you have an infinite loop on your hands is because you are comparing i to num, while also increasing num on every run. If you make sure i is always <= 10, you get your desired output:

Method 1

The reason why you have an infinite loop on your hands is because you are comparing i to num, while also increasing num on every run. If you make sure i is always <= 10, you get your desired output:

while num <= 10:
    i = 1
    while i <= 10:
        product = num*i
        print(num, " * ", i, " = ", product, "\n")
        i = i + 1
    num = num + 1
    print("\n")

Method 2

Even if the code you posted is not pythonic at all (it is very close to what could be written in C language), it nearly works: with minimum modifications, it can be fixed as follows to give your expected ouput:

numInput = int(input("Multiplication using value? : "))
num = 1

while num <= numInput:
    i = 1
    while i <= numInput:
        product = num*i
        print(num, " * ", i, " = ", product)
        i = i + 1
    print("")  # no need to add explicit newline character because it is automatically added
    num = num + 1

In a more pythonic way, you can also do the following:

numInput = int(input("Multiplication using value? : "))

for i in range(1,numInput+1):
    for j in range(1,numInput+1):
        print(i, " * ", j, " = ", i*j)
    print("")

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read