Print a multiplication table

Description: Print the table of 7 from 1 to 10 in the format 7 x 3 = 21.

Hint:

  • A for loop from 1 to 10 and printf.

Solution

public class Main {
    public static void main(String[] args) {
        int n = 7;
        for (int i = 1; i <= 10; i++) {
            System.out.printf("%d x %2d = %2d%n", n, i, n * i);
        }
    }
}
Output

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

How it works

  • %2d uses at least 2 places, so the numbers line up in columns.

Read Java for Loop.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.