Print multiplication table in Java
In this article, you will learn how to write a Java program to print multiplication table. Such a type of question is generally asked in programming interviews.
Java multiplication table using for loop
In the given Java program, we have used the for loop to print the multiplication table. First, we have taken the input number from the user. Then we have iterated 10 times using the for loop. In the initial iteration, the loop iterates and multiplies by 1 the given number. In the second iteration, 2 is multiplied by the given number, and so on.
import java.util.Scanner;
public class Multiplication_Table
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
System.out.println("Table of "+num+" -");
for(int i=1; i <= 10; i++)
{
System.out.println(num+" * "+i+" = "+num*i);
}
}
}
Output of the above code:
Enter number: 9
Table of 9 -
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
Enter number: 13
Table of 13 -
13 * 1 = 13
13 * 2 = 26
13 * 3 = 39
13 * 4 = 52
13 * 5 = 65
13 * 6 = 78
13 * 7 = 91
13 * 8 = 104
13 * 9 = 117
13 * 10 = 130
Java multiplication table using while loop
In the given Java program, we have used the while loop to print the multiplication table in Java. We have declared a variable i and initialized it by 1. Next, we will iterate the while loop until the value of i is smaller and equal to 10. In each iteration, the value of i is incremented by one and multiplied by the num variable. The loop is terminated when the value of i becomes greater than 10.
import java.util.Scanner;
public class Multiplication_Table
{
public static void main(String[] args)
{
int i =1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
System.out.println("Table of "+num+" -");
while(i<=10) {
num = num * 1;
System.out.println(num+" * "+i+" = "+num*i);
i += 1 ;
}
}
}
Output of the above code:
Enter number: 15
Table of 15 -
15 * 1 = 15
15 * 2 = 30
15 * 3 = 45
15 * 4 = 60
15 * 5 = 75
15 * 6 = 90
15 * 7 = 105
15 * 8 = 120
15 * 9 = 135
15 * 10 = 150
Java multiplication table using do-while loop
In the given Java program, we have used the do-while loop to print the multiplication table.
import java.util.Scanner;
public class Multiplication_Table
{
public static void main(String[] args)
{
int i =1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
System.out.println("Table of "+num+" -\n");
do
{
System.out.println(num+" * "+i+" = "+num*i);
i++; // incremented by 1
} while (i <= 10);
}
}
Output of the above code:
Enter number: 14
Table of 14 -
14 * 1 = 14
14 * 2 = 28
14 * 3 = 42
14 * 4 = 56
14 * 5 = 70
14 * 6 = 84
14 * 7 = 98
14 * 8 = 112
14 * 9 = 126
14 * 10 = 140
Java Program to Print Multiplication Table from 1 to 10
In the given Java program, we print the multiplication table from 1 to 10 using for loop.
import java.util.Scanner;
public class Multiplication_Table
{
public static void main(String[] args)
{
int i, j;
int num1 = 1, num2=10;
for (i = num1; i <= num2; i++)
{
num1 = i;
System.out.println("Table of "+num1+" -");
for(j = 1; j <=10; j++)
{
System.out.print(num1+" * "+j+" = "+num1 * j+"\n");
}
}
}
}
Output of the above code:
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
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
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
Table of 4 -
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
Table of 5 -
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Table of 6 -
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
Table of 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
Table of 8 -
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
Table of 9 -
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
Table of 10 -
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java