Keep it simple please, needs to be written in java, just need to instructions fo
ID: 3600882 • Letter: K
Question
Keep it simple please, needs to be written in java, just need to instructions followed.
Exercise 3: Write a program called Time Table2DArray to produce the multiplication table of 1 to 9 as shown using two nested for-loops and 2D array, i.e products in the table must be stored by using a 2D array. In the output table, each number takes a field of exact two digits (using printfO method) and the distance between two neighbouring numbers is a single whitespace. The output of the program should be: 112345 67 89 2 2 4 6 8 10 12 14 16 18 3 3 6 9 12 15 18 21 24 27 4 4 8 12 16 20 24 28 32 36 55 1e 15 20 25 30 35 40 45 6 6 12 18 24 30 36 42 48 54 71 7 14 21 28 35 42 49 56 63 8 8 16 24 32 40 48 56 64 72 9 9 18 27 36 45 54 63 72 81Explanation / Answer
Hello, I have created a TimeTable2DArray program to calculate and print multiplication table from 1 to 9. As per your requirement, I have made it simple and easy to understand. The rows and columns are formatted nicely using the printf() functions. Comments are included for every statements. Have a look, Thank you J
//TimeTable2DArray.java file
public class TimeTable2DArray {
public static void main(String[] args) {
/**
* creating a new array of 9*9 size
*/
int[][] array=new int[9][9];
/**
* filling the array elements with multiplication table data
*/
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
/**
* here the array index are from 0 to 9; thats why we are
* adding 1 to i and j;
*/
array[i][j]=(i+1)*(j+1); /*corresponding row * col*/
}
}
/**
* The following two statements will print * | 1 2 3 4 5 6 7 8 9
* to the screen
*/
System.out.print(" * | ");
for(int i=0;i<9;i++){
System.out.printf("%2d ",array[i][0]);
}
/**
* putting a line break
*/
System.out.println();
/**
* the following code is for printing
* the line with -------- (30 spaces wide)
*/
String special=String.format(" %30s", " ").replace(' ', '-');
System.out.println(special);
/**
* The below code will print the multiplication table
*/
for(int i=0;i<9;i++){
System.out.printf("%2d | ",array[i][0]); /*printing first column numbers in each loop*/
for(int j=0;j<9;j++){
System.out.printf("%2d ",array[i][j]);
}
System.out.println(); /*putting a line break*/
}
}
}
/*Output*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.