Write a program in java to display the multiplication table of a given integer

In this article, we will understand how to print a multiplication table. Multiplication table is created by iterating the required input 10 times using a for loop and multiplying the input value with numbers from 1 to 10 in each iteration.

Below is a demonstration of the same −

Input

Suppose our input is −

Input : 16

Output

The desired output would be −

The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Algorithm

Step 1 – START
Step 2 – Declare two integer values namely my_input and i.
Step 3 - Read the required values from the user/ define the values
Step 4 – Iterate from 1 to 10 using a for-loop, and in each iteration, multiply the numbers 1 to
10 with the input.
Step 5- Display the resulting value in after each iteration.
Step 6- Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool

Write a program in java to display the multiplication table of a given integer
.

import java.util.Scanner;
public class MultiplicationTable {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 16;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter a number : ");
      my_input = my_scanner.nextInt();
      System.out.println("The multiplication table of " +my_input + " is :");
      for(i = 1; i <= 10; ++i){
         System.out.printf("%d * %d = %d 
", my_input, i, my_input * i); } } }

Output

Required packages have been imported
A reader object has been defined
Enter a number : 16
The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class MultiplicationTable {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 16;
      System.out.println("The number is defined as " +my_input);
      System.out.println("The multiplication table of " +my_input + " is :");
      for(i = 1; i <= 10; ++i){
         System.out.printf("%d * %d = %d 
", my_input, i, my_input * i); } } }

Output

The number is defined as 16
The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

Following is a Java program which accepts an integer variable from user and prints the multiplication table of that particular integer.

Example

import java.util.Scanner;
public class MultiplicationTable {
   public static void main(String args[]) {
      System.out.println("Enter an integer variable :: ");
      Scanner sc = new Scanner(System.in);
      int num = sc.nextInt();
      for(int i=1; i<= 20; i++) {
         System.out.println(""+num+" X "+i+" = "+(num*i));
      }
   }
}

Output

Enter an integer variable ::
17
17 X 1 = 17
17 X 2 = 34
17 X 3 = 51
17 X 4 = 68
17 X 5 = 85
17 X 6 = 102
17 X 7 = 119
17 X 8 = 136
17 X 9 = 153
17 X 10 = 170
17 X 11 = 187
17 X 12 = 204
17 X 13 = 221
17 X 14 = 238
17 X 15 = 255
17 X 16 = 272
17 X 17 = 289
17 X 18 = 306
17 X 19 = 323
17 X 20 = 340

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class JavaExcercise {
 
   public static void main(String[] args)
 
{
   int j,num;
{
   System.out.print("Enter number of terms : ");
    Scanner in = new Scanner(System.in);
            num = in.nextInt();
 
   System.out.println ("\n");
   for(j=0;j<=num;j++)
 
     System.out.println(num+" X "+j+" = " +num*j);
   }
}
}

Result

Write a program in java to display the multiplication table of a given integer
Write a Java program to display the multiplication table of a given integer

How to write a program for multiplication table in Java?

Java Program to Print Multiplication Table.
import java.util.Scanner;.
public class Multiplication_Table..
public static void main(String[] args).
Scanner s = new Scanner(System. in);.
System. out. print("Enter number:");.
int n=s. nextInt();.

How to write multiplication table in Java using while loop?

Example 2: Using While Loop As you can see here, the iterator count starts from i=1 and goes all the way to i = range, which is 15 in this case. In each iteration, we are printing n * i = n*i, where n is the number for which multiplication table is being printed. Here n=9, and then we increase the iteration count by 1.

How to print a table of a number in Java?

Using Java for Loop.
import java.util.Scanner;.
public class TableExample..
public static void main(String args[]).
Scanner sc = new Scanner(System.in);.
System.out.print("Enter number: ");.
//reading a number whose table is to be print..

How to print in table format in Java?

FormatterExample.java.
import java.util.Formatter;.
public class FomatterExample..
public static void main(String args[]) throws Exception..
int num[] = {10, 21, 13, 4, 15, 6, 27, 8, 19};.
Formatter fmt = new Formatter();.
fmt.format("%15s %15s %15s\n", "Number", "Square", "Cube");.