Results 1 to 7 of 7
Thread: help with a Loop(arithmetic)
 01252012, 11:01 PM #1Noob
 Join Date
 Jul 2011
 Location
 MIyayo
 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?
 01262012, 12:47 AM #2Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 7
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 :)
 01262012, 02:50 AM #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.
Sincerely, Joshua Green
Please REP if I help :)
 01262012, 01:41 PM #4Noob
 Join Date
 Jul 2011
 Location
 MIyayo
 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; 01262012 at 01:46 PM.
 01262012, 01:58 PM #5Noob
 Join Date
 Jul 2011
 Location
 MIyayo
 Posts
 11
 Rep Power
 0
Re: help with a Loop(arithmetic)
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 lolLast edited by derb2k2; 01262012 at 02:08 PM.
 01262012, 02:09 PM #6Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 7
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!
 01262012, 02:15 PM #7Noob
 Join Date
 Jul 2011
 Location
 MIyayo
 Posts
 11
 Rep Power
 0
Similar Threads

Find the value of the arithmetic expression...
By hydride in forum Advanced JavaReplies: 3Last Post: 03162011, 10:17 AM 
Arithmetic using doubles
By Black_Eye in forum New To JavaReplies: 4Last Post: 10192010, 08:35 AM 
Abbreviated Arithmetic
By SweetLD215 in forum New To JavaReplies: 19Last Post: 10132010, 12:46 AM 
Polynomials Arithmetic
By thisisIT in forum New To JavaReplies: 3Last Post: 03092010, 02:27 PM 
Arithmetic Stacks
By unc123w in forum New To JavaReplies: 22Last Post: 10212008, 08:24 PM
Bookmarks