Exercise
Repeat until 0 (Use Do While)
Objetive
Write a java program that asks the user for a number "x" and displays 10*x. The program must repeat the process until the user enters 0, using "do-while".
Example Code
import java.util.Scanner; // Import Scanner class for input
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Declare a variable to store the number entered by the user
int x;
// Start a do-while loop that will repeat until the user enters 0
do {
// Ask the user to input a number
System.out.print("Enter a number (0 to stop): ");
// Read the input from the user
x = scanner.nextInt();
// Display the result of 10 * x
if (x != 0) {
System.out.println("10 * " + x + " = " + (10 * x));
}
} while (x != 0); // The loop continues as long as x is not 0
// Close the scanner to free up resources
scanner.close();
}
}