Hello, Please help write program in JAVA that prints out the Multiplication Tabl
ID: 3838821 • Letter: H
Question
Hello,
Please help write program in JAVA that prints out the Multiplication Table for a range of numbers (positive integers).
PROGRAM MUST DO THE FOLLOWING:
1) Prompt the user for a starting number: 'x'
2) Prompt the user for an ending number: 'y'
3) Print out the multiplication table of 'x' up to the number 'y'
SPECIFIC REQUIREMENTS
1) You must use the following method to load the array: public static void loadArray(int table[ ][ ], int x, int y)
2) You must use the following method to display the array: public static void printMultiplicationTable(int table[ ][ ], int x, int y)
3) You must load the table array with products of the factors. See screenshot of working program below:
In Figure 1 below, the 5 x 5 array is loaded with the products for the times tables from 5 to 8
In Figure 2 below, the 2 x 2 array is loaded with the products for the times tables from 5 to 6
CA. C: WindowsAsystem320cmd.exe Enter the starting value Enter the ending value 4 16 20 24 28 32 5 20 25 30 35 40 6 24 30 36 42 48 28 35 42 49 56 8 32 40 48 56 64 Press any key to continue Figure 1 CA. C:Windowslsystem32lcmd.exe Enter the starting value Enter the ending value 5 6 5 25 30 6 30 36 Press any key to continue Figure 2Explanation / Answer
MultiplicationTable.java
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
// Declaring variable
int x, y;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Getting the numbers entered by the user
System.out.print(" Enter the starting value :");
x = sc.nextInt();
System.out.print(" Enter the ending value :");
y = sc.nextInt();
//Creating an two dimensional array
int table[][] = new int[y - x + 1][y - x + 1];
//calling the method to load the array
loadArray(table, x, y);
//calling the method to display the array
printMultiplicationTable(table, x, y);
}
//Method implementation which displays the 2-D array
private static void printMultiplicationTable(int[][] table, int x, int y) {
System.out.print(" ");
for (int i = x; i <= y; i++) {
System.out.print(i + " ");
}
System.out.println();
System.out.print(" ");
for (int i = x; i <= y; i++) {
System.out.print("---" + " ");
}
System.out.println();
// Displaying the multiplication table
for (int i = 0; i < table.length; i++) {
System.out.print(i+x + " | ");
for (int j = 0; j < table[0].length; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
//Method implementation which fills the 2-D array with values
private static void loadArray(int[][] table, int x, int y) {
int k=x;
for (int i = x; i <= y; i++) {
for (int j = x; j <= y; j++) {
table[i-k][j-k] = i * j;
}
}
}
}
____________________
Output:
5 6
--- ---
5 | 25 30
6 | 30 36
_______________
Output#1:
Enter the starting value :4
Enter the ending value :8
4 5 6 7 8
--- --- --- --- ---
4 | 16 20 24 28 32
5 | 20 25 30 35 40
6 | 24 30 36 42 48
7 | 28 35 42 49 56
8 | 32 40 48 56 64
___________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.