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 22 of 22

Thread: New Recursion problem need lil help

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default New Recursion problem need lil help

    this program is supposed to add up the integers in a number entered by a user and print out the sum. i got the easy part down.

     if(number<10)
    {
    sum=number;
     }

    for the next part i was thinking about storing the value of each integer in an array by using mod operator then then addin them all up. any suggestions or psuedo code to help???


  2. #2
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    Changed my mind not gonna use an array, ive decided to do something like this but its not really working and i kno why but i dnt know how to change itm

    while(number>10)
    {
    digit=number%10;
    sum = digit; 
    }


    what i want to do is save the last digit so i can keep incrementing the sum value with whatever the integer is but idk how i can save the last integer pissibly a variable, but how???

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    Nvm all i got it

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: New Recursion problem need lil help

    Do you mean, if a user enters 10, it adds up 1 + 0?

    I'm a bit confused. If you still need help, please post your current code
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    Delstateprogramer (July 3rd, 2010)

  6. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    yes thats exactly it and I fear i thought i got it but i didnt still need some help lol

  7. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    public class AddEmUp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
             System.out.println("Enter an integer:");
                Scanner input=new Scanner(System.in);
           double number= input.nextDouble();
    adddigits(number);
                System.out.println("The sum of the integers in this digit is: "+ sum);
        }
        public static double sum=0;
        public static double digit=0;
      public static double tempsum;
    public static double adddigits(double number)/*Precondition:Positive number must be entered
                                            * Postcondition: The sum of the digits in number are
                                            * calculated and saved
                                            */
    {
        if(number<10)
        {
            sum=number+digit;
        }
        else
     
        {
            if(number>10)
            {
            digit=number/10;
     
            sum=digit;
     
            number=sum;
         adddigits(number);
     
            }
           return  sum;
     
            }
        return sum;
    }
    }
    Last edited by Json; June 29th, 2010 at 04:13 AM.

  8. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    thats the new code but yea its a logical error in there somewhere

  9. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: New Recursion problem need lil help

    Quote Originally Posted by Delstateprogramer View Post
    thats the new code but yea its a logical error in there somewhere
    Are you sure that you get your number when you use " / " maybe it is better this " % ".

  10. The Following User Says Thank You to Davidovic For This Useful Post:

    Delstateprogramer (July 3rd, 2010)

  11. #9
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: New Recursion problem need lil help

    OK perhaps if you explained a bit better what exactly you are trying to do. I am guessing you want to find the sum of the digits of an integer not the sum of integers of a digit first of all declare your variables above your main method I can't remember if you are allowed to declare them elsewhere but if you are it's still good practice to put them at the start of the class.

    Secondly I'm guessing that it can only be a 1 or 2 digit number(1 to 99) in which case you don;t have to do anything particullarly clever and you can do it with an if statement.

    So lets look at your code
        if(number<10)
        {
            sum=number+digit;
        }
        else
    This part: you are setting sum to number plus digit but what is digit at this stage? It should give the right result but it's not what you'd call good programming.

        else
     
        {
            if(number>10)
            {
            digit=number/10;
     
            sum=digit;
     
            number=sum;
         adddigits(number);
     
            }
           return  sum;
     
            }
        return sum;
    }

    This runs whenever it's greater then 10 anyways so you don't need the nested if statement(if you want to provide some checks say make sure the number is between 1 and 99 then that's a different story)

    But anyways this part
    digit = number/10
    Say the number is 57 that will make digit 5 fair enough but after that
            sum=digit;
            number=sum;
         adddigits(number);
    That code is will do the exact same thing as
    adddigits(digit)

    Because you are essantially setting sum to 5 and then number to five and calling that method. The mehtod call by the way is just going to run it as 5 and you probably end up getting 10 if I'm reading this right because you've set digit to 5 and you're passing the method 5 and it's the same object.

    But anyways why don't you write up a bit of pseudocode and see what exactly you want to do and then post it here and we can see if you understand the logic. From there we can give you some more advice. Also it's best if you provide 1 or 2 example inputs and outputs.

  12. The Following User Says Thank You to Faz For This Useful Post:

    Delstateprogramer (July 3rd, 2010)

  13. #10
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    I didnt intend for it to be a number 1 through 99. It would not matter what the user put in. and yes it would add the digits in an integer. Heres an example.

    If the user entered the integer 223 the output would be "The sum of the digits in this integer is 7"

    Heres a mild attempt at psuedo code


    if (number<10)
    sum= number

    else
    sum=sum of digits in number
    return sum

    or something like that

  14. #11
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    The user should be able to enter any integer and the program should output the sum of the digits in the integer.

  15. #12
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: New Recursion problem need lil help

    number = 224;

    When you see this number you know that sum is 8.. So how did you calculate it?

    1° number = 224

    (224 < 10) = false

    224 % 10 = 4 "digit = 4"

    (224 - 4)/10 = 22


    2° number = 22

    (22 < 10 ) = false

    22 % 10 = 2 "digit = 2"

    (22 - 2) / 10 = 2



    3° number = 2

    (2 < 10) = true "digit = 2"

    And after this you sum all digits. So my best advice for yo would be draw algorithm first for recursion then later code.

  16. #13
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    Quote Originally Posted by Davidovic View Post
    number = 224;

    When you see this number you know that sum is 8.. So how did you calculate it?

    1° number = 224

    (224 < 10) = false

    224 % 10 = 4 "digit = 4"

    (224 - 4)/10 = 22


    2° number = 22

    (22 < 10 ) = false

    22 % 10 = 2 "digit = 2"

    (22 - 2) / 10 = 2



    3° number = 2

    (2 < 10) = true "digit = 2"

    And after this you sum all digits. So my best advice for yo would be draw algorithm first for recursion then later code.
    how do i save the prior digits to add them up at the end becuz here they keep changing and are not saved so they cant be added at the end

  17. #14
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: New Recursion problem need lil help

    Quote Originally Posted by Delstateprogramer View Post
    how do i save the prior digits to add them up at the end becuz here they keep changing and are not saved so they cant be added at the end
    Can you post the code for that?

  18. #15
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: New Recursion problem need lil help

    Ah OK well then I see what you were doing with your code now, I think.

    But your pseudocode is quite bare you need to think about how to get each digit, if you knew the length it's easy enough but it's not that hard even when you don't.

    Well what you want to do is get the last digit of a number and then cut that digit out leaving the remaining digits and repeat until you have no digits left(or have 1 digit left and then add it to what you have).

    So think about how you would get the last digit of a number. Then how would you cut that digit out. You won't need to convert to a string or anything it can all be done with maths.

    Actually are you familiar with modulus (% <--Represented by that)?

    Example of what I was talking about:
    Current Number-->Last Digit------>Sum
    223-------------------->3-------------->3
    22--------------------->2-------------->5
    2----------------------->2-------------->7

    EDIT: Sorry didn't see the second page.

    You don't need to save the digits just keep a running sum. If you want to save them(however I really don't see the point an ArrayList may be in order.
    Last edited by Faz; June 30th, 2010 at 06:17 AM.

  19. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Recursion problem need lil help

    sum=sum of digits in number
    What is the pseudo code for "sum of digits in number"?
    That's just a restatement of what the whole program is supposed to do.
    Your recursion needs to get the 'current' digit and add to it the sum of digits for the remaining digits (ie without the current digit)

  20. #17
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: New Recursion problem need lil help

    In recursion, you want to "reduce" the problem into a simpler problem until the problem becomes so trivial you can instantly return the answer.

    So, take the number 1234

    sumOfDigits(1234) = 4 + sumOfDigits(123) // reduce the problem
    sumOfDigits(123) = 3 + sumOfDigits(12) // reduce the problem
    sumOfDigits(12) = 2 + sumOfDigits(1) // reduce the problem
    sumOfDigits(1) = 1 // Hey, we know the answer to this problem easily!
    // now time to setup back through the call stack
    sumOfDigits(12) = 2 + (1) = 3
    sumOfDigits(123) = 3 + (3) = 6
    sumOfDigits(1234) = 4 + (6) = 10 // final answer

    So, a simple recursive definition of the problem could look like this:
    function sumOfDigits(number)
       if(number < 10)
           return number
      else
           return number % 10 + sumOfDigits(number / 10)
    Last edited by helloworld922; July 1st, 2010 at 02:03 PM.

  21. The Following User Says Thank You to helloworld922 For This Useful Post:

    Faz (July 2nd, 2010)

  22. #18
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    Ok thx ill try that out

  23. #19
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    Tried this code and it didnt work

    if(number<10)
    {
    return number; 
    }
     else
    {
    return (number%10)+((addDigits(number/10)));
    }
    Last edited by helloworld922; July 3rd, 2010 at 10:07 PM.

  24. #20
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    Thank you for your help everyone i figured the problem out heres the code i used

    public static int addDigits(int number)
    {
    if(number<10)
    {
    sum=number+sum;
    }
    else
    {
    digit=number%;
    number=(number-digit)/10;
    sum=sum+digit;
    addDigits(number);
    }
    return sum;
    }
    Last edited by helloworld922; July 3rd, 2010 at 10:07 PM.

  25. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Recursion problem need lil help

    addDigits(number);
    You don't use the value returned by the addDigits() method called here?

  26. #22
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: New Recursion problem need lil help

    lol the code works and that all that matters

Similar Threads

  1. Recursion Problem Need Help ASAP
    By Delstateprogramer in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 26th, 2010, 08:36 PM
  2. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  3. Program without loops and recursion
    By jayaram in forum Algorithms & Recursion
    Replies: 7
    Last Post: April 1st, 2010, 09:02 AM
  4. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM
  5. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM