Exercise
Repeat until 0
Objetive
Write a java program to ask the user for a number "x" and display 10*x. It must repeat until the user enters 0 (using "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 while loop that continues as long as x is not 0
while (true) {
// 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();
// If the user enters 0, break the loop and stop the program
if (x == 0) {
break;
}
// Display the result of 10 * x
System.out.println("10 * " + x + " = " + (10 * x));
}
// Close the scanner to free up resources
scanner.close();
}
}