In this tutorial, we will learn to create a multiplication table of a given number using Java.
We can use the for loop or while loop for implementing easily.
Code for Multiplication Table
import java.util.Scanner;
public class demo {
public static void main(String args[]) {
int num;
System.out.print("Enter a number: ");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
for(int i = 0; i <= 10; i++) {
System.out.println(num + " * " + i + " = " + (num * i));
}
}
}
Output:
Enter a number: 14 14 * 0 = 0 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
Explanation of the Code
First we are importing the scanner class to enter a number.
Then we are creating a variable to store the number that is “num”.
int num;
Next we are asking the user to enter a number
System.out.print("Enter a number: ");
Next we are creating a scanner object to read input from the user.
Scanner sc = new Scanner(System.in) num = sc.nextInt();
Now we are creating a for loop for the generation of multiplication table and printing the table.
for (int i = 0; i <= 10; i++) {
System.out.println(num + " * " + i + " = " + (num * i));
This is how our code works in printing a multiplication table.