Python Program to Print Multiplication Table
This article is created to cover some programs in Python that prints multiplication table of a number entered by user at run-time. Here are the list of programs available in this article:
- Print multiplication table of a number using for loop
- Print multiplication table of a number using while loop
- Print multiplication table in given range
Print Multiplication Table of a Number using for Loop
The question is, write a Python program that prints multiplication table of a number using for loop. The number must be entered by user. The program given below is answer to this question:
print("Enter a Number: ") num = int(input()) k = 1 print("\nMultiplication Table:") for i in range(10): mul = num*k print(mul) k = k+1
Here is the initial output produced by this program:
Enter a number say 5 and press ENTER key to print the multiplication table as shown in the snapshot given below:
Modified Version of Previous Program
The end used in this program, to skip insertion of an automatic newline using print(). The str() method converts any type of value to a string type.
print(end="Enter a Number: ") num = int(input()) print("\nMultiplication Table of " +str(num)) for i in range(1, 11): print(str(num)+ " * " +str(i)+ " = " +str(num*i))
Here is its sample run with user input 8:
Print Table of a Number using while Loop
This program doesn't uses for loop, rather it uses while loop to do the same task as of previous program.
print(end="Enter a Number: ") num = int(input()) print("\nMultiplication Table of " +str(num)) i = 1 while i<11: print(str(num)+ " * " +str(i)+ " = " +str(num*i)) i = i+1
This program produces the same output as of previous program.
Print Multiplication Table in Given Range
This program is little different from above program. Yes, this program also prints multiplication table, but not of a single number. This can be decided by user. That is, the program allows user to enter the range to print multiplication table of all those numbers that comes in given range.
print(end="Enter the Range: ") s = int(input()) e = int(input()) if s==e: num = s print("----Multiplication Table of " +str(num)+ "----") for i in range(1, 11): print(str(num)+ " * " +str(i)+ " = " +str(num*i)) elif s>e: num = e while num<=s: print("----Multiplication Table of " +str(num)+ "----") for i in range(1, 11): print(str(num)+ " * " +str(i)+ " = " +str(num*i)) num = num+1 else: num = s while num<=e: print("----Multiplication Table of " +str(num)+ "----") for i in range(1, 11): print(str(num)+ " * " +str(i)+ " = " +str(num*i)) num = num+1
Here is its sample run with user input 3 and 5:
« Previous Program Next Program »
Liked this post? Share it!