Hello, Please help write program in JAVA that prints out the Multiplication Tabl
ID: 3838822 • 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
4) Code must be properly commented
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
/**
* The java program TableMultiplication
* that prompts user to enter staring
* and ending values and prints table
* of multiplication to console.
* */
//TableMultiplication.java
import java.util.Scanner;
public class TableMultiplication {
public static void main(String[] args) {
int x;
int y;
int table[][];
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
System.out.println("Enter starting value");
//prompt for x value
x=scanner.nextInt();
System.out.println("Enter ending value");
//prompt for y value
y=scanner.nextInt();
//creating table matrix
table=new int[y-x+1][y-x+1];
//calling loadArray method
loadArray(table, x, y-x+1);
//calling printMultiplicationTable method
printMultiplicationTable(table, x, y-x+1);
}
/**
* The method printMultiplicationTable that
* prints the table of multiplication with
* starting x and y values to console.
* */
public static void printMultiplicationTable(int table[ ][ ], int x, int y){
//starging lines
System.out.printf(" ");
for (int i = 0; i < y; i++) {
System.out.printf("%-4d",x+i);
}
//for dashed heading
char ch='-';
System.out.printf(" -------------");
for (int i = 0; i < 4*x; i++) {
System.out.printf("%c",ch);
}
System.out.println();
//print rows and columns of table
for (int row = 0; row < table.length; row++) {
System.out.printf("%-4d |",x+row);
for (int col = 0; col < y; col++) {
System.out.printf("%-4d",table[row][col]);
}
System.out.println();
}
}
/**
* The method loadArray takes two 2d table array and
* starging x and ending y values
* and stores the multiplication values of x and y
* into table array
* */
public static void loadArray(int table[ ][ ], int x, int y){
//for row values
for (int row = 0; row < table.length; row++) {
//for column values
for (int cols = 0; cols < y; cols++) {
//set product to table
table[row][cols]=(x+row)*(x+cols);
}
}
}
}
---------------------------------------------------------------------------------------------------------------
Sample Output:
Enter starting value
4
Enter 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
Enter starting value
5
Enter ending value
6
5 6
---------------------------------
5 |25 30
6 |30 36
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.