Write a Java Program to print multiplication table. Ask user to give the number
ID: 3810986 • Letter: W
Question
Write a Java Program to print multiplication table. Ask user to give the number multiplicand and the range of multipliers. FORMAT and display the table as output. After the output is displayed, ask the user if they want to continue with another table. If yes they need to enter a lower case y or an upper case Y, other wise to exit out press any other button.
Hint: Use Sentinel value as Loop Variable. Choose between do-while and while loop. Check for condition to be true on user input for Y or y.
Explanation / Answer
PROGRAM CODE:
package array;
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
char choice = 'y';
int multiplicand, multiplier;
Scanner keyboard = new Scanner(System.in);
while(choice == 'y' || choice == 'Y')
{
System.out.print(" Enter the multiplicand: ");
multiplicand = Integer.valueOf(keyboard.nextLine());
System.out.print("Enter the multiplier(range starts from 1): ");
multiplier = Integer.valueOf(keyboard.nextLine());
System.out.println("------------------MULTIPLICATION TABLE---------------");
for(int i=1; i<=multiplier; i++)
{
System.out.println(multiplicand + " X " + i + " = " + multiplicand*i);
}
System.out.print(" DO you want to continue? (y/n): ");
choice = keyboard.nextLine().charAt(0);
}
}
}
OUTPUT:
Enter the multiplicand: 23
Enter the multiplier(range starts from 1): 8
------------------MULTIPLICATION TABLE---------------
23 X 1 = 23
23 X 2 = 46
23 X 3 = 69
23 X 4 = 92
23 X 5 = 115
23 X 6 = 138
23 X 7 = 161
23 X 8 = 184
DO you want to continue? (y/n): y
Enter the multiplicand: 12
Enter the multiplier(range starts from 1): 5
------------------MULTIPLICATION TABLE---------------
12 X 1 = 12
12 X 2 = 24
12 X 3 = 36
12 X 4 = 48
12 X 5 = 60
DO you want to continue? (y/n): n