Java program to generate multiplication table
Here's a Java program to generate a multiplication table:
public class MultiplicationTable { public static void main(String[] args) { int n = 10; // number of rows and columns // nested for loop to print the multiplication table for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { System.out.printf("%4d", i * j); } System.out.println(); } } }
In this program, we have an integer variable n
that represents the number of rows and columns in the multiplication table. We use a nested for loop to print out the multiplication table. The outer loop runs from 1 to n
and controls the rows, while the inner loop runs from 1 to n
and controls the columns.
Inside the inner loop, we calculate the product of i
and j
(i.e., the value of the cell in the multiplication table) and use the printf()
method to format the output with a field width of 4. We then print a newline character after each row using the println()
method.
When you run this program, it will generate a multiplication table with n
rows and columns, where the value in each cell is the product of the row and column indices.