. Write a Java program that will use a two-dimensional array to store the multip
ID: 3692749 • Letter: #
Question
. Write a Java program that will use a two-dimensional array to store the multiplication table for numbers between 0 and n (input from user, range 3 - 30 --> validate input!). Use two methods (to generate the multiplication table and print.) Generate output similar to the sample output below.Sample Output:
Do you want to start(Y/N): y
Enter an integer n (3 - 30): 32
ERROR! Valid range: 3 - 30! REENTER: 13
0 to 13 multiplication table
============================
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 2 4 6 8 10 12 14 16 18 20 22 24 26
0 3 6 9 12 15 18 21 24 27 30 33 36 39
0 4 8 12 16 20 24 28 32 36 40 44 48 52
0 5 10 15 20 25 30 35 40 45 50 55 60 65
0 6 12 18 24 30 36 42 48 54 60 66 72 78
0 7 14 21 28 35 42 49 56 63 70 77 84 91
0 8 16 24 32 40 48 56 64 72 80 88 96 104
0 9 18 27 36 45 54 63 72 81 90 99 108 117
0 10 20 30 40 50 60 70 80 90 100 110 120 130
0 11 22 33 44 55 66 77 88 99 110 121 132 143
0 12 24 36 48 60 72 84 96 108 120 132 144 156
0 13 26 39 52 65 78 91 104 117 130 143 156 169
Do you want to continue(Y/N): y
Enter an integer n (3 - 30): 8
0 to 8 multiplication table
============================
0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8
0 2 4 6 8 10 12 14 16
0 3 6 9 12 15 18 21 24
0 4 8 12 16 20 24 28 32
0 5 10 15 20 25 30 35 40
0 6 12 18 24 30 36 42 48
0 7 14 21 28 35 42 49 56
0 8 16 24 32 40 48 56 64
Do you want to continue(Y/N): n
Explanation / Answer
MultiplecationTable1.java
public class MultiplecationTable1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
while(true){
System.out.println("Enter an integer n (3 - 30):");
int n = in.nextInt();
if(n > 2 && n <31){
int a[][] = new int[n+1][n+1];
tableCalculatot(a);
System.out.println("0 to "+n+" multiplication table");
tableDisplay(a);
System.out.println("Do you want to continue(Y/N): ");
char c = in.next().charAt(0);
if(c == 'n' || c == 'N')
break;
}
else{
System.out.println("ERROR! Valid range: 3 - 30!");
}
}
}
public static void tableCalculatot(int a[][]){
for(int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++){
a[i][j] = (i*j) ;
}
}
}
public static void tableDisplay(int a[][]){
String str = "";
System.out.println("============================");
for(int i=0; i<a.length; i++){
str = "";
for(int j=0; j<a[i].length; j++){
str = str + a[i][j] +" ";
}
System.out.println(str+" ");
}
}
}
Output:
Enter an integer n (3 - 30):
32
ERROR! Valid range: 3 - 30!
Enter an integer n (3 - 30):
13
0 to 13 multiplication table
============================
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 2 4 6 8 10 12 14 16 18 20 22 24 26
0 3 6 9 12 15 18 21 24 27 30 33 36 39
0 4 8 12 16 20 24 28 32 36 40 44 48 52
0 5 10 15 20 25 30 35 40 45 50 55 60 65
0 6 12 18 24 30 36 42 48 54 60 66 72 78
0 7 14 21 28 35 42 49 56 63 70 77 84 91
0 8 16 24 32 40 48 56 64 72 80 88 96 104
0 9 18 27 36 45 54 63 72 81 90 99 108 117
0 10 20 30 40 50 60 70 80 90 100 110 120 130
0 11 22 33 44 55 66 77 88 99 110 121 132 143
0 12 24 36 48 60 72 84 96 108 120 132 144 156
0 13 26 39 52 65 78 91 104 117 130 143 156 169
Do you want to continue(Y/N):
y
Enter an integer n (3 - 30):
8
0 to 8 multiplication table
============================
0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8
0 2 4 6 8 10 12 14 16
0 3 6 9 12 15 18 21 24
0 4 8 12 16 20 24 28 32
0 5 10 15 20 25 30 35 40
0 6 12 18 24 30 36 42 48
0 7 14 21 28 35 42 49 56
0 8 16 24 32 40 48 56 64
Do you want to continue(Y/N):
n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.