Java program to print multiplication table of any number

Let’s learn java program to print multiplication table of any number.

Java program to print multiplication table of any number

Java program to print multiplication table of a given number is the most asked interview question.

Also read – polymorphism in java

To print multiplication table for any number user enters a number as input using nextInt() method of Scanner class.

In the next step using for loop from one to ten to generate multiplication of user entered number.

Now let’s see program to print multiplication table of any number.

import java.util.Scanner;
// java program to print multiplication table of any number
public class MultiplicationTablesInJava
{
   public static void main(String[] args)
   {
      System.out.println("Please enter an integer to print tables : ");
      Scanner sc = new Scanner(System.in);
      int number = sc.nextInt();
      System.out.println("Multiplication table of " + number + " is : ");
      for(int a = 1; a <= 10; a++)
      {
         System.out.println(number + " * " + a + " = " + number * a);
      }
      sc.close();
   }
}


Output:

java program to print multiplication table of any number

Print multiplication table for any range

Also we can print multiplication table for any range (between two numbers). Here’s the code.

import java.util.Scanner;
public class MultiplyTable
{
   public static void main(String[] args)
   {
      int firstNum, secondNum, a, b;
      System.out.println("Please enter number of any range: ");
      Scanner sc = new Scanner(System.in);
      firstNum = sc.nextInt();
      secondNum = sc.nextInt();
      for(a = firstNum; a <= secondNum; a++)
      {
         System.out.println("Multiplication table of : " + a);
         for(b = 1; b <= 10; b++)
         {
            System.out.println(a + " * " + b + " = " + (a * b));
         }
      }
      sc.close();
   }
}


Output:

Please enter number of any range:
1
3
Multiplication table of : 1
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10

Multiplication table of : 2
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

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 in java using array

Let’s learn to print multiplication table in java using array.

Here’s multiplication table in java using two-dimensional array.

public class MultiplicationTableUsingArray
{
   public static void main(String[] args)
   {
      int[][] arrMultipleTable = new int[10][10];
      int row = 1, column = 1;
      for(int a = 0; a < arrMultipleTable.length; a++)
      {
         for(int b = 0; b < arrMultipleTable[a].length; b++)
         {
            arrMultipleTable[a][b] = row * column;
            column = column + 1;
         }
         row = row + 1;
         column = 1;
      }
      for(int a = 0; a < arrMultipleTable.length; a++)
      {
         for(int b = 0; b < arrMultipleTable[a].length; b++)
         {
            System.out.print(" " + arrMultipleTable[a][b] + "\t| ");
         }
         System.out.print("\n");
      }
   }
}


Output:

java program to print multiplication table of any number

multiplication table in java using nested for loop

Let’s learn multiplication table in java using nested for loop.

public class UsingNestedForLoop
{
   public static void main(String[] args)
   {
      System.out.print("Multiplication table using nested for loop: \n");
      for(int a = 1; a <= 10; a++)
      {
         for(int b = 1; b <= 10; b++)
         {
            System.out.print(a * b + "\t");
         }
         System.out.println();
      }
   }
}


Output:

java program to print multiplication table of any number