File tree Expand file tree Collapse file tree
section-05-arrays/src/main/java Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class MultiplicationTableDemo {
4+
5+ public static void main (String [] args ) {
6+
7+ // Prompt the user for number of rows and columns
8+ Scanner scanner = new Scanner (System .in );
9+ System .out .print ("How many rows? " );
10+ int numRows = scanner .nextInt ();
11+
12+ System .out .print ("How many columns? " );
13+ int numCols = scanner .nextInt ();
14+
15+ scanner .close ();
16+
17+ // Initialize the array based on the rows and columns
18+ int [][] table = new int [numRows ][numCols ];
19+
20+ // Compute multiplication table values
21+ for (int row =0 ; row < numRows ; row ++) {
22+
23+ for (int col =0 ; col < numCols ; col ++) {
24+ // use (row+1) to give values 1 to numRows
25+ // similar thing for (col+1)
26+ table [row ][col ] = (row +1 ) * (col +1 );
27+ }
28+
29+ }
30+
31+ System .out .println ();
32+
33+ // Print out the results
34+ for (int row =0 ; row < numRows ; row ++) {
35+
36+ for (int col =0 ; col < numCols ; col ++) {
37+ System .out .print (table [row ][col ] + "\t " );
38+ }
39+ System .out .println ();
40+
41+ }
42+ }
43+ }
44+
45+
46+
47+
48+
49+
50+
51+
52+
You can’t perform that action at this time.
0 commit comments