Multiplication Table in Java
A Multiplication table is a mathematical table that defines multiplication operations for a number with a range. Multiplication table in Java can be implemented with a for loop or a while loop.
Introduction
We all have been writing multiplication tables in our primary classes. A multiplication table is a mathematical table that defines a number's multiplication operation or product operation with a range. So the multiplication table for the number "2" with a range 1 to 10 will look like this:
As you can see here, the number "2" is multiplied by the numbers ranging from 1 to 10.
So our program's first task will be to take a number as input and in the second step, we will use a loop to multiply this number by 1 to 10. We can use for loop as well as while loop to iterate over the range of 1 to 10. You can adjust this range by changing the 10 with the desired range end limit or you can read this range end limit from the user.
Example 1: Using For Loop
Output:
As you can see here, the for loop starts from i=1 and goes to i=10. In each iteration, we print n * i = n*i, where n is the number for which the multiplication table is printed. Here n=9.
Example 2: Using While Loop
In this example, we will read two numbers; one is the number for which the multiplication table is to be generated, and the other number is the range end limit. Like in the previous example, Using For Loop, the range end limit was set to 10.
Output:
As you can see here, the iterator count starts from i=1 and goes all the way to i = range, which is 15 in this case. In each iteration, we are printing n * i = n*i, where n is the number for which multiplication table is being printed. Here n=9, and then we increase the iteration count by 1.
Conclusion
- The multiplication table defines the product operation of a number with a range.
- To iterate over the range, we can use for loop and while loop. In each iteration, we will print the number, the iteration count, and the product of the number with the iteration count.