Java Program to Generate Multiplication Table

In this post, we will learn to code the Java Program to Generate Multiplication Table. Let’s understand the multiplication table and then we will see How to Generate a Multiplication table of any number in Java Programming Language.

We will solve the problem using both For Loop and while Loop.

Now, Let’s see the Java Program to Generate Multiplication Table using For Loop.

Java Program to Generate Multiplication Table

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number : ");
        int num = sc.nextInt();
        
        System.out.println("The Multiplication Table of "+num+" is :");
        for(int i=1; i<=10; i++){
            int ans = num * i;
            System.out.println(""+num+" X "+i+" = "+ans);
        }
        
    }
    
}

Output

Enter the Number : 5

The Multiplication Table of 5 is :

5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number : ");
        int num = sc.nextInt();

In this Program, first, we take the input of the number we want to generate the multiplication table for from the user as input using the Scanner Class in Java.

        System.out.println("The Multiplication Table of "+num+" is :");
        for(int i=1; i<=10; i++){
            int ans = num * i;
            System.out.println(""+num+" X "+i+" = "+ans);
        }

Then, using for loop where i increments from 1 to 10, we calculate the product of the number and i and display it the multiplication table order.

In first iteration, i = 1, num = 5, then we calculate 5 * i = 5 * 1 = 5 and print it using the System.out.println(""+num+" X "+i+" = "+ans) function. i.e. 5 X 1 = 5.

In second iteration, i = 2, then ans = 5 * i = 5 * 2 = 10 and we print it. Similary the for loop goes on till i is 10 and our program generates the multiplication of the given number.

This is the Java Program to Generate Multiplication Table using For Loop.

Java Program to Generate Multiplication Table Using While Loop

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number : ");
        int num = sc.nextInt();
        
        System.out.println("The Multiplication Table of "+num+" is :");
        
        int i = 1;
        while( i <= 10 ){
            
            int ans = num * i;
            System.out.println(""+num+" X "+i+" = "+ans);
            ++i;
            
        }
        
    }
    
}

Output

Enter the Number : 13

The Multiplication Table of 13 is :

13 X 1 = 13
13 X 2 = 26
13 X 3 = 39
13 X 4 = 52
13 X 5 = 65
13 X 6 = 78
13 X 7 = 91
13 X 8 = 104
13 X 9 = 117
13 X 10 = 130

This is the Java program to Generate Multiplication Table Using While Loop.

Conclusion

I hope after going through this post, you understand the Java Program to Generate Multiplication Table. We solved the problem using both For Loop and while Loop.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.