# Do Loop, Reversing a Number Example, Incorrect Code in Book?

• October 23rd, 2011, 07:09 PM
djl1990
Do Loop, Reversing a Number Example, Incorrect Code in Book?
Im working through Java Software Solutions by Lewis and Loftus, Im struggling to come to terms with something simple. firstly here is the program:

Code :

```public class ReverseNumber {   // reverses the digits of an integer mathematically public static void main(String[] args) {   int number, lastDigit, reverse = 0;   Scanner scan = new Scanner(System.in);   System.out.println("Enter a positive integer: "); number = scan.nextInt(); { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0) System.out.println("That number reverse is " + reverse);   } }```

And i really cant get my head around the following line (i know its probably really simple) :

reverse = (reverse * 10) + lastDigit;

'reverse' is earlier declared as 0 in the program right?

So my question is what is the point in (reverse * 10) i.e. (0 * 10)
• October 23rd, 2011, 07:58 PM
Henry518
Re: Do Loop, Reversing a Number Example, Incorrect Code in Book?
Lets put an example. Let's say you input the number 5741, here's how it is processed:

5741

lastDigit = 5741 % 10
lastDigit = 1

reverse = 0*10 + 1
reverse = 1

number = 5741 / 10
number = 574

When you repeat the process it will be like this:

lastDigit = 574 % 10
lastDigit = 4

reverse = 1*10 + 4
reverse = 10 + 4 = 14

If you continue you'll realize that "reverse" is actually taking a form (the reverse form of "number")

The (0*10) is not pointless, because it's actually (reverse*10) + lastDigit. It will be 0 + "first reversed digit".
• October 24th, 2011, 06:02 AM
Mr.777
Re: Do Loop, Reversing a Number Example, Incorrect Code in Book?
Quote:

Originally Posted by djl1990
Im working through Java Software Solutions by Lewis and Loftus, Im struggling to come to terms with something simple. firstly here is the program:

Code :

```public class ReverseNumber {   // reverses the digits of an integer mathematically public static void main(String[] args) {   int number, lastDigit, reverse = 0;   Scanner scan = new Scanner(System.in);   System.out.println("Enter a positive integer: "); number = scan.nextInt(); { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0) System.out.println("That number reverse is " + reverse);   } }```

And i really cant get my head around the following line (i know its probably really simple) :

reverse = (reverse * 10) + lastDigit;

'reverse' is earlier declared as 0 in the program right?

So my question is what is the point in (reverse * 10) i.e. (0 * 10)

Reverse is also assigned the value. So, it's not compulsory for reverse to be always 0. Reverse's value is being changed everytime, depending upon the input you have made.
So, don't say if this code is incorrect.
• October 24th, 2011, 07:01 AM
djl1990
Re: Do Loop, Reversing a Number Example, Incorrect Code in Book?
Thanks a lot.

Good explanations.