Results 1 to 7 of 7
  1. #1
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default 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. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default 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 :)

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

    Default 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 12:46 PM.

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

    Default Re: help with a Loop(arithmetic)

    Quote Originally Posted by eRaaaa View Post
    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 01:08 PM.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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. #7
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default Re: help with a Loop(arithmetic)

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

Similar Threads

  1. Find the value of the arithmetic expression...
    By hydride in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2011, 09:17 AM
  2. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  3. Abbreviated Arithmetic
    By SweetLD215 in forum New To Java
    Replies: 19
    Last Post: 10-13-2010, 12:46 AM
  4. Polynomials Arithmetic
    By thisisIT in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 01:27 PM
  5. Arithmetic Stacks
    By unc123w in forum New To Java
    Replies: 22
    Last Post: 10-21-2008, 08:24 PM

Posting Permissions

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