Time to use loops in a real problem! Write a program that takes a number from the user and prints it in reverse using a while loop.
YOUR TASK
Create a program that:
Asks the user for a number
Uses a loop to reverse its digits
Prints the result as Reversed: 4321
Need a hint?
Step-by-step:
1. Read the number: int num = sc.nextInt();
2. Use a while loop: while (num != 0)
3. Extract last digit: int digit = num % 10;
4. Build reversed: reversed = reversed * 10 + digit;
5. Remove digit: num /= 10;
6. Print: System.out.println("Reversed: " + reversed);
Example (input: 1234):
Enter a number: 1234
Reversed: 4321
Code Editor Ready
Output
Console
Console initialized...
Ready for Project 3: Reverse Number
> Test your code with the "Check My Code" button
> When it works, the Complete button will activate!