Exercise
Multiplication table
Objetive
Write a java program to ask the user for a number and display its multiplication table, like this:
Please enter a number:
5
The multiplication table for 5 is:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Example Code
// Importing the Scanner class to handle input from the user
import java.util.Scanner;
public class Program {
// Main method, entry point of the program
public static void main(String[] args) {
// Create a scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Please enter a number: ");
int number = scanner.nextInt();
// Display the multiplication table for the entered number
System.out.println("The multiplication table for " + number + " is:");
for (int i = 1; i <= 10; i++) {
int result = number * i;
System.out.println(number + " x " + i + " = " + result);
}
// Close the scanner
scanner.close();
}
}