Exercise
Multiplication table (use while)
Objetive
Write a java program that prompts the user to enter a number and displays its multiplication table using a 'while' loop.
Example Code
// Importing the Scanner class to handle input from the user
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Declare a variable for the number
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt(); // Read the number from the user
// Declare a variable to track the multiplier
int multiplier = 1;
// Loop to display the multiplication table
while (multiplier <= 10) {
// Display the result of the multiplication
System.out.println(number + " x " + multiplier + " = " + (number * multiplier));
multiplier++; // Increment the multiplier for the next iteration
}
}
}