Java multiplication table

Hey guys!! Welcome to flower brackets blog. Today we will learn java multiplication table.

Java Multiplication Table

In java, we can write java program to print multiplication table for any number. Now let’s see an example,

Also read – java program to find prime number

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


In the same lines 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

Related Posts