Java Program to Generate Multiplication Table

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to print a multiplication table. Multiplication table is created by iterating the required input 10 times using a for loop and multiplying the input value with numbers from 1 to 10 in each iteration.

Below is a demonstration of the same −

Input

Suppose our input is −

Input : 16

Output

The desired output would be −

The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Algorithm

Step 1 – START
Step 2 – Declare two integer values namely my_input and i.
Step 3 - Read the required values from the user/ define the values
Step 4 – Iterate from 1 to 10 using a for-loop, and in each iteration, multiply the numbers 1 to
10 with the input.
Step 5- Display the resulting value in after each iteration.
Step 6- Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class MultiplicationTable {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 16;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter a number : ");
      my_input = my_scanner.nextInt();
      System.out.println("The multiplication table of " +my_input + " is :");
      for(i = 1; i <= 10; ++i){
         System.out.printf("%d * %d = %d \n", my_input, i, my_input * i);
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter a number : 16
The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class MultiplicationTable {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 16;
      System.out.println("The number is defined as " +my_input);
      System.out.println("The multiplication table of " +my_input + " is :");
      for(i = 1; i <= 10; ++i){
         System.out.printf("%d * %d = %d \n", my_input, i, my_input * i);
      }
   }
}

Output

The number is defined as 16
The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160
raja
Updated on 21-Feb-2022 12:43:21

Advertisements