![]() |
VOOZH | about |
Given a number n, we need to print its table.
Examples :
Input: 5
Output:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50Input: 2
Output:
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
Table of Content
The iterative approach for printing a multiplication table involves using a loop to calculate and print the product of a given number and the numbers in range from 1 to 10. In this method, you begin with the number whose table you want to print and use a loop to multiply it with increasing values.
Output :
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Time Complexity - O(1)
Space Complexity - O(1)
Illustration
Step by step execution of loop for the multiplication table of
n = 5.We have
n = 5, and the loop will iterate fromi = 1toi = 10.First Iteration (
i = 1):
- The loop multiplies
n = 5byi = 1.- Result:
5 * 1 = 5.- Output:
5 * 1 = 5.Second Iteration (
i = 2):
- The loop multiplies
n = 5byi = 2.- Result:
5 * 2 = 10.- Output:
5 * 2 = 10.Third Iteration (
i = 3):
- The loop multiplies
n = 5byi = 3.- Result:
5 * 3 = 15.- Output:
5 * 3 = 15.....
....Tenth Iteration (
i = 10):
- The loop multiplies
n = 5byi = 10.- Result:
5 * 10 = 50.- Output:
5 * 10 = 50.
In this method, we pass i as an additional parameter with initial value as 1. We print n * i and then recursively call for i+1. We stop the recursion when i becomes 11 as we need to print only 10 multiples of given number and i.
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
Time Complexity - O(1)
Space Complexity - O(1)