🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

WAP to print multiplication table in Java  


Logical Steps:
  • For multiplication table of any number, follow these steps:
    1. Take a number.
    2. Use a loop to generate the table:
      • Use a for loop to iterate from 1 to 10.
      • For each iteration, multiply the input number by the loop counter.
    3. Print the multiplication in any format (number * 1 = result) for each iteration.
    4. Print the result.
Program:
  • Below is the simple program:
    public class MultiplicationTable
    {
        public static void main(String[] args)
        {
            int no = 7;
    
            System.out.println("Multiplication Table of " + no + ":");
    
            // Loop from 1 to 10 to generate the table
            for (int i = 1; i <= 10; i++)
            {
                int result = no * i;
                System.out.println(no + " x " + i + " = " + result);
            }
        }
    }
    Output:
    Multiplication Table of 7:
    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
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class MultiplicationTable
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            // Prompt the user to enter a number
            System.out.print("Enter a number to print its multiplication table: ");
            int no = scanner.nextInt();
    
            System.out.println("\nMultiplication Table of " + no + ":");
    
            // Loop from 1 to 10 to generate the table
            for (int i = 1; i <= 10; i++)
            {
                int result = no * i;
                System.out.println(no + " x " + i + " = " + result);
            }
    
            // Close the scanner
            scanner.close();
        }
    }
    Output:
    Enter a number to print its multiplication table: 10
    
    Multiplication Table of 10:
    10 x 1 = 10
    10 x 2 = 20
    10 x 3 = 30
    10 x 4 = 40
    10 x 5 = 50
    10 x 6 = 60
    10 x 7 = 70
    10 x 8 = 80
    10 x 9 = 90
    10 x 10 = 100
Program Explanation:
  • Import Scanner Class:
    • The Scanner class is imported to take user input.
  • Declare a Variable:
    • An integer variable (no) is declared to store the user’s input.
  • Take Input:
    • The program prompts the user to enter a number for the multiplication table.
    • scanner.nextInt() is used to read and store the input in the no variable.
  • Use a for Loop:
    • The loop starts from i = 1 and iterates up to i = 10.
    • For each iteration, it calculates the product of no and i.
    • It prints the result in the format: no x i = product.
  • Display the Multiplication Table:
    • The program prints the multiplication table for the entered number up to 10.
  • Close Scanner:
    • The scanner.close() method is used to close the input stream and release resources.