Results 1 to 3 of 3
  1. #1
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Question Recursion adding the digits of an integer

    Hey guys I was working on this practice problem from a website I solved it but I don't understand how it works. I have tried retracing it but it makes no sense to me. The point of the problem was to add the digits of an int recursively. (for example integer 123 = 3+2+1 = 6)
    Java Code:
    public int sumDigits(int n) 
    {
       if(n<= 9)
          return n;
       return sumDigits(n/10)+sumDigits(n%10);   
    }
    I do understand that n/10 removes the rightmost digit and n%10 removes the leftmost digit.
    Thanks.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Recursion adding the digits of an integer

    Use a piece of paper to work through the steps that the code does when it executes.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Recursion adding the digits of an integer

    Suppose I have a very intelligent but very shy friend; he can do what you want (add the digits of a number); I am just a bluffing guy who also claims he can do it; aamof, I can only add one single digit to a number; basically, this is me:

    Java Code:
    int meBluffing(int number) {
       if (number < 10) return number;
       return myShyFriend(number/10)+number%10;
    What I basically do is take the last digit from the number, let my friend do the hard work (if there is anything left to do) and add my last digit to what my friend returned. Here's the clue: I'm schizophrenic: I have no friend, I made him up; so this is basically me:

    Java Code:
    int meBluffing(int number) {
       if (number < 10) return number;
       return meBluffing(number/10)+number%10;
    As you can see from the above, this method is a recursive method; basically identical to your method (it has another name, but that isn't relevant).

    kind regarrds,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 10-27-2011, 05:23 PM
  2. Adding To An Integer
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 07-21-2011, 03:59 AM
  3. adding integer to the JPanel
    By navid in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 04:26 PM
  4. Digits of an integer.
    By Allgorythm in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 02:34 AM
  5. Counting digits in an integer value, including zero
    By lithium002 in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 04:56 AM

Tags for this Thread

Posting Permissions

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