Java program to print multiplication table for any number

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

Java program to print multiplication table for any number

Java program to print multiplication table using for loop is one of the common interview question.

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

Now this user entered number is stored in integer variable ‘number’. In the next step using for loop, loop from one to ten and print multiplication table of user entered number.

Now let’s see program to print multiplication table for any number using for loop.

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.println(number + " * " + a + " = " + number * a);
      }
      sc.close();
   }
}

Output:

java program to print multiplication table of any number

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