import java.util.Scanner;
//For Any Number
import java.util.Scanner;
class Tables
{
public static void main(String args[])
{
int a, b, c, d;
System.out.println("Enter range of numbers to print their multiplication table");
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
for (c = a; c <= b; c++)
{
System.out.println("Multiplication table of "+c);
for (d = 1; d <= 10; d++)
{
System.out.println(c+"*"+d+" = "+(c*d));
}
}
}
}
Code Explanation:
1. Scanner Class Import:
import java.util.Scanner;
This imports the Scanner class, which is used to read input from the user.
2. Class Definition:
class Tables
This defines a class named Tables. Inside this class is the main method, which contains the code that will be executed when the program runs.
3. Main Method:
public static void main(String args[])
Here, the entry point of the program is the main method. Any code written in this method gets executed whenever the program begins.
4. Variable Declaration:
int a, b, c, d;
Here four integer variables have been declared
- a and b are used to store the range (start and end) of the multiplication tables.
- c is used in the outer loop to iterate over the numbers in the given range.
- d is used in the inner loop to print the multiplication results for each number.
5. Taking Input from User:
System.out.println(“Enter range of numbers to print their multiplication table”);
This line prints a message asking the user to enter the range of numbers for which multiplication tables will be printed.
6. Creating Scanner Object:
Scanner in = new Scanner(System.in);
This line declares a Scanner object called in, which reads input from the user.
7. Reading the Range Values:
a = in.nextInt();
b = in.nextInt();
These lines of code read two integers from the user. The numbers will be used to determine the start number, a, and the end number, b, for the multiplication table range.
8. Outer Loop (for numbers between a and b):
for (c = a; c <= b; c++)
- This is the outer for loop, which iterates over the numbers from a to b (inclusive).
- The loop variable c represents the current number for which the multiplication table will be printed.
9. Printing the Multiplication Table Header:
System.out.println(“Multiplication table of ” + c);
This line prints the header for the multiplication table of the current number c.
10. Inner Loop (for multiplication from 1 to 10):
for (d = 1; d <= 10; d++)
- This is the nested for loop, looping over the integers from 1 to 10. These produce the values of the multiples for the current c.
- The loop variable is d, so d is the factor to multiply c.
11. Printing the Results of Multiplications:
System.out.println(c +\”*\” + d +\” = \” +(c *d));
This line prints the result of multiplying the current number c by d, in the format “c * d = result”.
12. End of Program:
When the inner loop has completed printing the multiplication table for one number, the outer loop moves on to the next number and performs the same action as done above. Once all tables are printed, the program terminates.
Example Walkthrough:
The user will input the range 3 through 5. The program then prints the multiplication tables of 3, 4, and 5.
Sample Input:
Enter range of numbers to print their multiplication table
3
5
Sample Output:
Multiplication table of 3
3*1 = 3
3*2 = 6
3*3 = 9
3*4 = 12
3*5 = 15
3*6 = 18
3*7 = 21
3*8 = 24
3*9 = 27
3*10 = 30
Multiplication table of 4
4*1 = 4
4*2 = 8
4*3 = 12
4*4 = 16
4*5 = 20
4*6 = 24
4*7 = 28
4*8 = 32
4*9 = 36
4*10 = 40
Multiplication table of 5
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
Output Explanation:
The input of the user is a starting number, 3, and an ending number, 5. This program will print out one multiplication table after another as shown below. For every number in the range given, 3, 4, and 5, it prints out a multiplication table from 1 to 10.
For each multiplication table, the number is stated clearly with its results for multiplication on numbers from 1 to 10.
Summary
- The program will ask for a range of numbers and it will print the multiplication tables for all the numbers in this range.
- It makes use of nested for loops: the outer loop iterates over each number in the range, and the inner one will generate the multiplication results from 1 to 10 for that number.