Write a JAVA program which: Prints out the Multiplication Table for a range of n
ID: 3832608 • Letter: W
Question
Write a JAVA program which:
Prints out the Multiplication Table for a range of numbers (positive integers).
• Prompts the user for a starting number: say 'x'
• Prompts the user for an ending number: say 'y'
• Prints out the multiplication table of 'x' up to the number 'y'
Sample Output:
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. For example:
a. In Figure 1, the 2 x 2 array is loaded with the products for the times tables from 5 to 6.
b. In Figure 2, the 5 x 5 array is loaded with the products for the times tables from 5 to 8.
4. If a String input is entered, it should display something like this:
GENERAL RESTRICTIONS:
1. No infinite loops, examples include:
a. for(;;)
b. while(1)
c. while(true)
d. do{//code}while(1);
2. No break statements to exit loops
Enter the starting value Enter the ending value 6 5 25 30 6 30 36 Press any key to continue Figure 1Explanation / Answer
Please find my implementation.
import java.util.Scanner;
public class MultiplicationTable {
public static void loadArray(int table[][], int s, int e){
for(int i=s,a=0; a<=table.length-1; i++, a++){
//System.out.print(i+" | ");
for(int k=s, b=0; b<=table.length-1; k++,b++)
table[a][b] = i*k;
}
}
public static void printMultiplicationTable(int table[][], int s, int e){
System.out.print(" ");
for(int i=s; i<=e; i++)
System.out.print(i+" ");
System.out.println(" -------------------------------------------");
for(int i=s,a=0; a<=table.length-1; i++, a++){
System.out.print(i+" | ");
for(int b=0; b<=table.length-1; b++)
System.out.print(table[a][b]+" ");
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the starting value: ");
int s = sc.nextInt();
System.out.print("Enter the ending value: ");
int e = sc.nextInt();
int n = e-s+1;
int[][] table = new int[n][n];
loadArray(table, s, e);
printMultiplicationTable(table, s, e);
sc.close();
}
}
/*
Sample run:
Enter the starting value: 2
Enter the ending value: 7
2 3 4 5 6 7
------------------------------------
2 | 4 6 8 10 12 14
3 | 6 9 12 15 18 21
4 | 8 12 16 20 24 28
5 | 10 15 20 25 30 35
6 | 12 18 24 30 36 42
7 | 14 21 28 35 42 49
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.