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 :33 x 1 = 33 x 2 = 63 x 3 = 93 x 4 = 123 x 5 = 153 x 6 = 183 x 7 = 213 x 8 = 243 x 9 = 273 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 :33 x 1 = 33 x 2 = 63 x 3 = 93 x 4 = 123 x 5 = 153 x 6 = 183 x 7 = 213 x 8 = 243 x 9 = 273 x 10 = 30
Using While Loop
num = int(input("Enter the Number :"))
i = 1while(i<=10):
print("{} x {} = {}".format(num,i,num*i))
i += 1
Output
Enter the Number :44 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 364 x 10 = 40
To learn more about While loops in Python reading the following articles is recommended.