Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    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)


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default 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".

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Do Loop, Reversing a Number Example, Incorrect Code in Book?

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

    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.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Do Loop, Reversing a Number Example, Incorrect Code in Book?

    Thanks a lot.

    Good explanations.

Similar Threads

  1. My AP CS Book Has Code Written in JRE5
    By RAWBERRY in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2011, 07:55 PM
  2. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  3. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM
  4. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 11:36 AM
  5. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM