Python program to print multiplication table from 1 to 10 using while loop

This is my code, it outputs a multiplication table but it's not what I wanted!

num = int(input("Multiplication using value? : ")) while num <= 10: i = 1 while i <= num: product = num*i print(num, " * ", i, " = ", product, "\n") i = i + 1 print("\n") num = num + 1

I am basically creating a multiplication table from the user's input from 1-9.

Ex. If the user inputs "3", I should get this output:

1*1=1 1*2=2 1*3=3 2*1=2 2*2=4 2*3=6 3*1=3 3*2=6 3*3=9

Tomerikoo

16.6k15 gold badges37 silver badges54 bronze badges

asked Jul 5, 2018 at 9:04

3

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")

answered Jul 5, 2018 at 9:32

0

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("")

answered Jul 5, 2018 at 9:22

Laurent H.Laurent H.

5,9761 gold badge17 silver badges37 bronze badges

For this problem it's easier to use for loops.

num = int(input("Multiplication using value? : ")) for left in range(1,num+1): # 1st loop for right in range(1,num+1): # 2nd loop (nested) print(left, " * ", right, " = ", left * right) print() # newline

To understand this problem, look at the two multiplicands: left and right.

Left multiplicand goes from (1-->num), hence the first for loop.

Then, for each value of the left multiplicand, the right multiplicand goes from (1-->num), hence the 2nd loop is nested inside the first loop.

answered Jul 5, 2018 at 9:30

bda01bda01

211 bronze badge

You've lots of logical error. Please have a look at this updated code:

num = int(input("Multiplication using value : ")) i=1 #you haven't initialized this variable while i <=num: j=1 while j <= num: product = i*j #updated print(i, " * ", j, " = ", product, "\n") #updated j = j + 1 print("\n") i = i + 1

Output (for input 3):

1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9

answered Jul 5, 2018 at 9:12

Taohidul IslamTaohidul Islam

5,1003 gold badges23 silver badges38 bronze badges

In Python 3.6+, you can use f-strings with a nested for loop:

num = int(input("Multiplication using value? : ")) for i in range(1, num+1): for j in range(1, num+1): print(f'{i} * {j} = {i*j}') Multiplication using value? : 3 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9

answered Jul 5, 2018 at 9:36

jppjpp

151k31 gold badges256 silver badges317 bronze badges

multiplication table using while loop in python

num = int(input("enter the number= ")) i = 1 while i<=10: print(num, "X", i, "=", num * i) i = i+1

output

enter the number= 33 33 X 1 = 33 33 X 2 = 66 33 X 3 = 99 33 X 4 = 132 33 X 5 = 165 33 X 6 = 198 33 X 7 = 231 33 X 8 = 264 33 X 9 = 297 33 X 10 = 330

funie200

3,4325 gold badges20 silver badges32 bronze badges

answered Dec 2, 2020 at 7:54

1

num = int(input("Enter the number: ")) i = 1 print("Mulltiplication of number:", num) while i<=10: print(f"{num}X{i}={num*i}") i = i + 1

answered Aug 29, 2021 at 1:31

0

Multiplication table 1 to 10

for x in range(1,11): for y in range(1,11): print(x*y, end='\t') print() print()

Eric Aya

69.1k34 gold badges176 silver badges245 bronze badges

answered Jan 24, 2019 at 16:48

1

input any number to get your normal multiple table(Nomenclature) in 10 iterate times.

num = int(input("Input a number: ")) # use for loop to iterate 10 times for i in range(1,11): print(num,'x',i,'=',num*i)

answered Apr 10, 2019 at 19:19

num = int(input('Enter the number you want the multiplication table for:')) i=1 while i<=10: product = num*i print(product) i=i+1 print('Thank you')

answered Jul 4, 2019 at 11:36

SumitSumit

11 bronze badge

Creating a multiplication table using while loop is shown below:

b = int(input('Enter the number of the multiplicaction table : ')) print('The multiplication table of '+ str(b) + 'is : ') a=0 while a<=11: a=a+1 c= a*b print( str(b)+' x '+str(a)+' ='+str(c)) print('done!!!!')

Shmn

6511 gold badge4 silver badges20 bronze badges

answered Oct 24, 2020 at 6:28

0

To create a multiplication table in python:

name=int(input("Enter the number of multiplication")) i=1 while(i<=10): print(str(name)+"X"+str(i)"="+str(name*i)) i=i+1

answered Jul 13, 2021 at 15:05

How do you code a multiplication table in Python?

Code using a for loop.
ourNum = int(input("Enter the number you want to generate a multiplication table for, then hit the `enter` key: ")).
ourRange = range(1,6).
for x in ourRange:.
result = ourNum * x..
print(ourNum," * ",x," = ",result).

How do you multiply a number in a while loop?

write a c program to print multiplication table of any number using for loop.
#include <stdio.h>.
int n, i;.
printf("Enter a Number ");.
scanf("%d",&n);.
while(i<=10){.
printf("%d * %d = %d \n", n, i, n*i);.

How do you print a multiplication table?

To print multiplication table we need to iterate from 1 to 10. Run a loop from 1 to 10 , increment 1 on each iteration. The loop structure should look like for(i=1; i<=10; i++) . Inside loop generate multiplication table using num * i and print in specified format.

Chủ đề