Exercise:
Write a Java Program to reverse a number.
1.Reverse using a while loop
Click Here to View the Solution!
public
class Reverse{
public static void main(String[] args) {
int num =
123456, reversed =
0;
while(num !=
0) {
int digit = num %
10;
reversed = reversed *
10 + digit;
num /=
10;
}
System.out.println(
"Reversed Number: " + reversed);
}
}
Click Here to View the Output!
Reversed Number: 654321
Click Here to View the Explanation!
- This program is used for reversing a number by using a while loop and then printing the number.
- Initially, two integer variables
num
and reversed initialized asnum = 123456
andreversed = 0
. - In the beginning, the value of num is divided by 10 (
num % 10
) and its remainder is stored in the variable digit. The remainder of 1234 when divided by 10 is 6. So, now digit = 6. - In the body of the loop, the reverse of num is formed by multiplying the value of the variable reversed with 10 that will insert a new tenth place in the reversed number. Similarly, if you multiply tenth place with 10, it’ll give you a hundredth place. The above digit containing the remainder will be added into reversed. Considering this formula, the variable reversed will now hold 0 * 10 + 6= 6.
- The value in
num 123456
is then divided by 10 to remove the last digit. Hencenum
will now holdnum = 12345
. - In the second iteration, the variable digit will hold 6 which is the remainder and the variable reversed will hold 6 * 10 + 5 = 65. The value in the variable num will be again divided by 10 and now
num = 1234
. - In the third iteration, the variable digit will hold 4 which is the remainder and the variable reversed will hold 65 * 10 + 4 = 654. The value in the variable num will be again divided by 10 and now
num = 123
. - In the fourth iteration, the variable digit will hold 3 which is the remainder and the variable reversed will hold 654 * 10 + 3 = 6543. The value in the variable num will be again divided by 10 and now
num = 12
. - In the fifth iteration, the variable digit will hold 2 which is the remainder and the variable reversed will hold 6543 * 10 + 2 = 65432. The value in the variable num will be again divided by 10 and now
num = 1
. - In the sixth iteration, the variable digit will hold 1 which is the remainder and the variable reversed will hold 65432 * 10 + 1 = 654321. The value in the variable num will be again divided by 10 and now
num = 0
. - The condition in the while loop is checked in each iteration. And when it will become false (
num = 0
), the loop will end. - The variable reversed will hold the reverse of num (
reversed = 654321
) and will be printed.
2.Reverse using a for loop
Click Here to View the Solution!
public
class Reverse {
public static void main(String[] args) {
int num =
12345, reversed =
0;
for(;num !=
0; num /=
10) {
int digit = num %
10;
reversed = reversed *
10 + digit;
}
System.out.println(
"Reversed Number: " + reversed);
}
}
Click Here to View the Output!
Reversed Number: 54321
Click Here to View the Explanation!
- This program is used to reverse a number by using a
for loop
and displaying the reverse number. - Like the above program, it perform the number reversal process however, it uses a for loop instead of a while loop which holds no initialization, a condition (
num != 0
) and an expression that will update the value ofnum
(num/10
). - In each iteration, a remainder will be calculated and stored in the variable digit. Which will be added into the variable reversed after multiplying it to 10.
- Until the (
num!=0
) condition remains true, the iterations will continue. - In the end, the reversed number of
num
will be stored in the variable reversed and displayed on the screen.