posted 3 weeks ago
I wrote this Java program to reverse a number using a while loop. Is this the correct approach, or is there a more efficient way to do this in Java?
class ReverseNumber {
public static void main(String[] args) {
int num = 1234;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number = " + reversed);
}
}