# Thread: help with a Loop(arithmetic)

1. Noob
Join Date
Jul 2011
Location
M-I-yayo
Posts
11
Rep Power
0

## help with a Loop(arithmetic)

premise of this is to reverse any number(<10 digits) entered by user and print it.

int number, lastDigit, reverse = 0;

number = scan.nextInt();

do
{
lastDigit = number % 10;
reverse = (reverse * 10) + lastDigit;
number = number / 10;
}
while (number > 0);

print reverse...

What i'm having trouble with is the logic of the arithmetic. e.g. "number" entered is 678, then lastDigit = 8. Considering the reverse equation, then what does "reverse" equal to and how is it able to reverse the number entered?

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
9

## Re: help with a Loop(arithmetic)

1.loop:
-----
lastDigit = 678%10 = 8
reverse = 0*10 + 8 = 8
number = 678/10 = 67

2.loop
-----
lastDigit = 67%10 = 7
reverse = 8*10 + 7 = 87
number = 67/10 = 6

3.loop
-----
lastDigit = .....
......
.....
was that your question? sorry if not :-)

3. ## Re: help with a Loop(arithmetic)

I have another question for you, did you write the arithmetic yourself or was this a code given to you to play around with to make it work? I keep running through the arithmetic in my head and it doesn't seem like it would work to me, so I just wanted to make sure before I say anything else.

4. Noob
Join Date
Jul 2011
Location
M-I-yayo
Posts
11
Rep Power
0

## Re: help with a Loop(arithmetic)

this is from a Java book. Java software solutions by Lewis and Loftus. The figures are the same as in the book. The program returns and answer of 876

Eraa I'm looking into your reply now. Thank you.
Last edited by derb2k2; 01-26-2012 at 01:46 PM.

5. Noob
Join Date
Jul 2011
Location
M-I-yayo
Posts
11
Rep Power
0

## Re: help with a Loop(arithmetic)

Originally Posted by eRaaaa
1.loop:
-----
lastDigit = 678%10 = 8
reverse = 0*10 + 8 = 8
number = 678/10 = 67

2.loop
-----
lastDigit = 67%10 = 7
reverse = 8*10 + 7 = 87
number = 67/10 = 6

3.loop
-----
lastDigit = .....
......
.....
was that your question? sorry if not :-)
Ah so the second loop takes number from the first loop, and then implements that and so on? I see the logic of it now. But
doesn't the bold equation( 678/10 ) equal 67 remainder 8? What do you do with the 8 when using 67 in the second loop?

okay so "number" is an int? is that why number = 678/10 = 67 ends up 67? forgive my noobishness lol
Last edited by derb2k2; 01-26-2012 at 02:08 PM.

6. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
9

## Re: help with a Loop(arithmetic)

number is an integer, so there is no remainder! 678/10 -> number = 67
System.out.print(678/10) - the remainder (the eight) is saved in lastDigit with the help of the modulo operator!

7. Noob
Join Date
Jul 2011
Location
M-I-yayo
Posts
11
Rep Power
0

## Re: help with a Loop(arithmetic)

tytytyt I will run through this once again after I get some food in me xD

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•