Java program to print multiplication table of any number

Today we will learn java program to print multiplication table of any number.

Java program to generate multiplication table

In java, we can write java program to print multiplication table for any number.

Also read – java program to print alphabets

In the below java program user enters a number as input. In the next step “for loop” is used to generate multiplication of given number.

Now let’s see an example,

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("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


Similarly we can execute 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


Also we can print multiplication table for any range (between two number). 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("Enter any two number : ");
      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:

Enter any two number :
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