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 the mathematical tables from 1 to 10 in java user enters a number as input.

In the next step for loop is used to generate multiplication of given number.

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

import java.util.Scanner;
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.printf("%d * %d = %d \n", number, a, number * a);
      }
      sc.close();
   }
}


Output:

java program to print multiplication table of any number

java program for multiplication table from 1 to 10 using while loop

Similarly we can execute java program for multiplication table from 1 to 10 using while loop.

Here’s java program for multiplication table from 1 to 10 using while loop or multiplication table from 1 to 10 using while loop.

import java.util.Scanner;
public class MultiplicationTables
{
   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("Table of " + number + " is : ");
      int a = 1;
      while(a <= 10)
      {
         System.out.printf("%d * %d = %d \n", number, a, number * a);
         a++;
      }
      sc.close();
   }
}


Output:

Please enter an integer to print tables : 6
Table of 6 is :
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60


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 array.

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


Output:

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |20 |
3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 |30 |
4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |40 |
5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45| 50 |
6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54| 60 |
7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63| 70 |
8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 |
9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |
10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 |