asked 190k views
4 votes
Design a Java program that uses nested loops to display a multiplication table for numbers 1 through 12.

1 Answer

2 votes

Answer:

Here's an example Java program that uses nested loops to display a multiplication table for numbers 1 through 12:

public class MultiplicationTable {

public static void main(String[] args) {

// Loop through the rows of the table

for (int i = 1; i <= 12; i++) {

// Loop through the columns of the table

for (int j = 1; j <= 12; j++) {

// Calculate the product of the row and column

int product = i * j;

// Display the product padded with spaces

System.out.printf("%4d", product);

}

// Move to the next row

System.out.println();

}

}

}

In this program, we use two nested for loops to iterate through the rows and columns of the multiplication table. The outer loop iterates over the rows, while the inner loop iterates over the columns.

For each cell in the table, we calculate the product of the row and column using the * operator. We then use the printf() method to display the product padded with spaces, so that all the numbers line up in columns. The format specifier %4d tells printf() to display the integer argument in a field with a width of 4 characters, padded with spaces as needed.

After each row is printed, we move to the next line using the println() method to create a new row in the table.

answered
User AKG
by
8.1k points