Reputation:
Could you explain me how this code works? I have tried it with any input and it always gives right result. I think that they key its the line reversenum = reversenum * 10;
but i need some explanation on it.
public static void main(String args[]) {
int num=123456789;
int reversenum =0;
while( num != 0 ){
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of specified number is: "+reversenum);
}
Upvotes: 0
Views: 218
Reputation: 2907
You start with 123456789 (num
) and 0 (reversenum
). Then, you multiply reversenum
by 10: it remains 0. num % 10
is the remainder when num
is divided by 10: this is 9, which becomes reversenum
. num
is then divided by 10, but integer division gets the floor of fractional results, so you will get 12345678 as num
(NOT 12345678.9). In the second pass, 9 (reversenum
) is multiplied by 10 to get 90, and the last digit of num
is added: 8. reversenum
becomes 98 and num
becomes 1234567. This keeps going until num
is 0 and reversenum
is 987654321; the while
loop's condition will then be satisfied.
Upvotes: 1
Reputation: 23897
At the end of each pass through the while loop, the current last digit of num
is removed and it becomes the last digit of reversenum
. So the last digit of num
is removed and it becomes the first digit added to reversenum
(and thus the first digit of reversenum
). Then it takes what was originally the next to last digit of num
and it becomes the second digit added to reversenum
(and thus the second digit of reversenum
). This continues until no digits are left to move.
Let's look at the first pass:
reversenum
becomes 0*10, which is 0. Then you add num%10
, which is 9. So reversenum
becomes 9.
Meanwhile the integer division makes num
become 12345678
Looking at the next pass:
reversnum
becomes 9*10 which is 90, then add num%10
which is 8, so reversenum
becomes 98.
Meanwhile integer division makes num
become 1234567.
small warning
What would you think the reverse of 90 is? You should be aware of this case.
Upvotes: 1
Reputation: 413
Inside the while statement it is multiplying the reversenum variable by ten, then adding the remainder of the num/10 to reversenum, and then finally it divides the num by ten. It is possible for this while statement to end because since each variable is an int, dividing num by ten will always produce the rounded up version of num/10 if num is not evenly divisible by 10.
Upvotes: 0
Reputation: 2757
Let your number be 12345. We extract the digits from the end. First we find the remainder when 12345 divided by 10. The remainder comes to be 5.Now we add this to our reversenum which is supposed to store the reverse number. Then we divide 12345 by 10 . As a integer divided by a integer gives a integer value . Therefore
num = 12345/10 = 1234
We multiply reversenum by 10 so that reversenum becomes 50 and again add the remainder to reversenum So 2nd time reversenum becomes 54.Then we extract the last digit of 1234 (num variable). As you can see this continues till the total number is reversed i.e, num=0 (there are no more digits to be extracted).
Upvotes: 0
Reputation: 34609
Essentially, what the code does is:
Upvotes: 1