In this Java program, you’ll learn how to Generate a Multiplication Table using the Java programming language.
How to Generate Multiplication Table in JAVA?
Example 1: using for loop
RUN CODE SNIPPETpublic class Main { public static void main(String[] args) { int num = 10; for(int i = 1; i <= 20; ++i) { System.out.printf("%d * %d = %d \n", num, i, num * i); } } }
OUTPUT
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 10 * 11 = 110 10 * 12 = 120 10 * 13 = 130 10 * 14 = 140 10 * 15 = 150 10 * 16 = 160 10 * 17 = 170 10 * 18 = 180 10 * 19 = 190 10 * 20 = 200
Example 2: using while loop
RUN CODE SNIPPETpublic class Main { public static void main(String[] args) { int num = 2, i = 1; while(i <= 10) { System.out.printf("%d * %d = %d \n", num, i, num * i); i++; } } }
OUTPUT
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
In the above program, the value of i is incremented inside the body of the loop.