Multiplication tables in Java


The Java program takes two user inputs, a number and a limit, and prints the multiplication table of the given number up to the given limit.

  • The program starts by importing the Scanner class from the java.util package, which is used to take user input from the console.
  • Then, the program creates a Scanner object in to read input from the console. The user is prompted to enter the table to be displayed and the limit up to which the table should be printed.
  • Inside the for loop, the program prints each row of the multiplication table using the current value of the loop variable i. The result of the multiplication is calculated using the expression (t * i).

Overall, the program looks correct and should print the multiplication table of the given number up to the given limit.

Source Code

import java.util.Scanner;
//Write a program to print the multiplication tables
public class multiplication {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter The Table : ");
        int t = in.nextInt();
        System.out.print("Enter The Limit : ");
        int n = in.nextInt();
        for(int i=1;i<=n;i++)
        {
            System.out.println(t + " x " + i + " = " + (t * i));
        }
    }
}
 

Output

Enter The Limit : 10
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
To download raw file Click Here

Basic Programs