Multiplication table program 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 interview.
Java multiplication table using for loop
Here, 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 MultiplicationTable {
public static void main(String[] args) {
int num;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number: ");
num = scanner.nextInt();
for(int i = 1; i <= 10; ++i)
{
System.out.printf("%d * %d = %d \n", num, i, num * i);
}
}
}
Output of the above code:
Enter the 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
Enter the number: 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
Java multiplication table using while loop
In the given Java program, we have used the while loop to print the multiplication table. 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 MultiplicationTable {
public static void main(String[] args) {
int i = 1;
int num;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number: ");
num = scanner.nextInt();
while(i <= 10)
{
System.out.printf("%d * %d = %d \n", num, i, num * i);
i++;
}
}
}
Output of the above code:
Enter the number: 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
Java program to print Multiplication Table (1 to 5)
In the given Java program, we print the multiplication table from 1 to 5 using the for loop.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
for(int j = 1; j <= 5; j++) {
for(int i = 1; i <= 10; i++)
{
System.out.printf("%d * %d = %d \n", j, i, j * i);
}
}
}
}
Output of the above code:
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
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
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
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
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
Related Articles
Enum from string JavaJava enum
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program 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