Hello, Please help write program in JAVA that prints out the Multiplication Tabl
ID: 3838820 • 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
import java.util.Scanner;
/**
*
* @author Sam
*/
public class MultiplicationTable {
public static void loadArray(int[][] table, int x, int y) {
for (int i = x; i <= y; i++)
for (int j = x; j <= y; j++)
table[i-x][j-x] = i * j;
}
public 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();
for (int i = x; i <= y + 1; i++)
System.out.print( "--------");
System.out.println("--");
for (int i = x; i <= y; i++) {
System.out.print(" " + i + "|");
for (int j = x; j <= y; j++)
System.out.print(" " + table[i-x][j-x]);
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting value:");
int x = sc.nextInt();
System.out.println("Enter the ending value:");
int y = sc.nextInt();
int[][] table = new int[y-x+1][y-x+1];
loadArray(table, x, y);
printMultiplicationTable(table, x, y);
}
}
This piece of code will serve your purpose. Let me know what you feel.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.