Java Program to Print Multiplication Table

Write a Java Program to Print Multiplication Table using For Loop, While Loop, and functions with an example. In each scenario, we must use the nested loops, one for the actual number and the other for the multiplication purpose (traverse from 1 to 10).

Java Program to Print Multiplication Table using For Loop

This program allows the user to enter any integer value and prints the multiplication table from that number to 9 using For Loop.

import java.util.Scanner;

public class MultiplicationTable {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	

		for(i = number; i < 10; i++)
		{
			for(j = 1; j <= 10; j++)
			{
				System.out.println(i +"  *  " + j + "  =  " + i * j);
			}
			System.out.println("================");
		}
	}
}
Java Program to Print Multiplication Table 1

Within this example, the first For loop is to iterate from the user entered value to 9. Next, we used Nested For Loop to iterate j from 1 to 10. The user entered the value number  = 9

First For Loop – First Iteration: for(i = 9; i < 10; i++). Here, the Condition i < 10 is True. So, it enters into the second For Loop.

Second For Loop – First Iteration: for(j = 1; 1 <= 10; 1++). The Condition is True. So, the statement inside the Java system.out.println printed.

Second For Loop – Second Iteration: for(j = 2; 2 <= 10; 2++). The Condition is True. So, the statement inside it was printed.

The second For Loop repeats the process until j reaches 11. Because if j is 11, the condition fails, so it exits from the second loop.

First For Loop – Second Iteration: for(i = 10; i < 10; i++). The Condition (10 < 10) Fails. So, it exits from the First For Loop.

Java Program to Print Multiplication Table using While Loop

This example is the same as above, but we are using the While Loop. Don’t forget the j++ and i++ lines; otherwise, you will end up in an infinite loop.

import java.util.Scanner;

public class Example1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int i, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		i = sc.nextInt();	

		while(i < 10)
		{
			j = 1;
			while(j <= 10)
			{
				System.out.println(i +"  *  " + j + "  =  " + i * j);
				j++;
			}
			System.out.println("================");
			i++;
		}
	}
}
 Please Enter any Number : 7
7  *  1  =  7
7  *  2  =  14
7  *  3  =  21
7  *  4  =  28
7  *  5  =  35
7  *  6  =  42
7  *  7  =  49
7  *  8  =  56
7  *  9  =  63
7  *  10  =  70
================
8  *  1  =  8
8  *  2  =  16
8  *  3  =  24
8  *  4  =  32
8  *  5  =  40
8  *  6  =  48
8  *  7  =  56
8  *  8  =  64
8  *  9  =  72
8  *  10  =  80
================
9  *  1  =  9
9  *  2  =  18
9  *  3  =  27
9  *  4  =  36
9  *  5  =  45
9  *  6  =  54
9  *  7  =  63
9  *  8  =  72
9  *  9  =  81
9  *  10  =  90
================

Java Program to Print Multiplication Table using function

This program is the same as the first example. But we separated the multi logic and placed it in a separate method so that you can use it in various locations.

import java.util.Scanner;

public class MulTab2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	

		printMulTab(number);
	}
	
	public static void printMulTab(int num)
	{
		int i, j;
		
		for(i = num; i < 10; i++)
		{
			for(j = 1; j <= 10; j++)
			{
				System.out.println(i +"  *  " + j + "  =  " + i * j);
			}
			System.out.println("================");
		}
	}
}
Java Program to Print Multiplication Table using functions