C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Program to Display the Multiplication TableIn Python, the user can write the program to display the multiplication table of any number. In this tutorial, we will discuss different methods for printing the multiplication table of any number using Python. Method 1: Using For loopIn the following example, we will print the multiplication table of any number (from 1 to 10) by using the for loop. Example: number = int(input ("Enter the number of which the user wants to print the multiplication table: ")) # We are using "for loop" to iterate the multiplication 10 times print ("The Multiplication Table of: ", number) for count in range(1, 11): print (number, 'x', count, '=', number * count) Output: Enter the number : 10 Enter the number of which the user wants to print the multiplication table: 13 The Multiplication Table of: 13 13 x 1 = 13 13 x 2 = 26 13 x 3 = 39 13 x 4 = 52 13 x 5 = 65 13 x 6 = 78 13 x 7 = 91 13 x 8 = 104 13 x 9 = 117 13 x 10 = 130 Explanation: In the above program, we have taken an input integer number from the user. Then we have iterated for loop using the range (1, 11) function, which means greater than or equal to 1 and less than 11. In the first iteration, the loop will iterate and multiply by 1 to the given number. In the second iteration, 2 is multiplied by the given number, and so on. In our case, we have printed the table of 10. You can provide the different numbers to test the program. Method 2: By using While LoopIn this method, we will use the while loop for printing the multiplication table of any number specified by the user. The following is the example for method 2: Example: number = int(input ("Enter the number of which the user wants to print the multiplication table: ")) count = 1 # we are using while loop for iterating the multiplication 10 times print ("The Multiplication Table of: ", number) while count <= 10: number = number * 1 print (number, 'x', i, '=', number * count) count += 1 Output: Enter the number of which the user wants to print the multiplication table: 27 The Multiplication Table of: 27 27 x 10 = 27 27 x 10 = 54 27 x 10 = 81 27 x 10 = 108 27 x 10 = 135 27 x 10 = 162 27 x 10 = 189 27 x 10 = 216 27 x 10 = 243 27 x 10 = 270 Explanation: The above code is the same as the previous program, but we have used the while loop. We declared a variable "count" and initialized it by 1. The while loop will iterate until the value of "count" is smaller than and equal to 10. Each time loop is iterated, the value of "count" will be incremented by 1. When the "count" became greater than 10, the loop will be terminated. ConclusionIn this tutorial, we have discussed two different methods which can be used for printing the multiplication table of any number using Python.
Next TopicPython Fibonacci Sequence
|