Python programs to display the multiplication table of a number have been shown here. Both the iterative and recursive approaches have been covered below.
1. Python program & output to display multiplication table of a number using iteration
Code has been copied
########################################## # alphabetacoder.com # Python Program to display multiplication # table of a given number using iteration ########################################## # take input of a number n = int(input("Enter a number: ")) r = int(input("Enter the range: ")) print("Multiplication table of", n, ":") # display table for i in range(1, r + 1): print(n, "x", i, "=", n * i)
Output
Enter a number: 14
Enter the range: 8
Multiplication table of 14 :
14 x 1 = 14
14 x 2 = 28
14 x 3 = 42
14 x 4 = 56
14 x 5 = 70
14 x 6 = 84
14 x 7 = 98
14 x 8 = 112
In this code, we use the input() function to take input from the user for the number n and the range r. Then, we display the header for the multiplication table.
Next, we use a for loop to iterate through the numbers from 1 to r (inclusive) and calculate the multiplication result (n * i).
Next, we use a for loop to iterate through the numbers from 1 to r (inclusive) and calculate the multiplication result (n * i).
2. Python program & output to display multiplication table of a number using recursion
Code has been copied
########################################## # alphabetacoder.com # Python Program to display multiplication # table of a given number using recursion ########################################## # recursive function to display # multiplication table def multiplication_table(n, r): # exit condition of recursive call if r == 0: return # call function multiplication_table(n, r - 1) # display the line print(n, "x", r, "=", n * r) # main function def main(): # take input of a number n = int(input("Enter a number: ")) r = int(input("Enter the range: ")) print("Multiplication table of", n, ":") # call recursive function to # display multiplication table multiplication_table(n, r) # driver code main()
Output
Enter a number: 17
Enter the range: 10
Multiplication table of 17:
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170
- The multiplication_table() function takes two parameters, n (the number for which the multiplication table is generated) and r (the range of the table).
- The function begins with an exit condition check. If the value of r is 0, the function returns, effectively ending the recursive calls and stopping further execution. This condition serves as the base case for the recursion.
- If the exit condition is not met, the function proceeds to make a recursive call to multiplication_table() with the arguments n and r - 1. This recursive call reduces the value of r by 1 in each iteration.
- After the recursive call, the function prints the line representing a single multiplication operation in the table. It displays the equation n x r = n*r using commas for separation.
- The main() function serves as the entry point of the program. It takes user input for the number n and the range r of the multiplication table.
- It then prints a header indicating the number for which the multiplication table is being generated.
- Finally, the main() function calls the multiplication_table() function with the input values of n and r, initiating the recursive generation of the multiplication table. The recursive approach allows the code to generate the multiplication table in a descending order of the range. The base case ensures that the recursion stops when the range reaches 0.