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

# Thread: New Recursion problem need lil help

1. ## 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. ## 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. ## Re: New Recursion problem need lil help

Nvm all i got it

4. ## 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

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

Delstateprogramer (July 3rd, 2010)

6. ## 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. ## 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();
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;

}
return  sum;

}
return sum;
}
}```

8. ## Re: New Recursion problem need lil help

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

9. ## Re: New Recursion problem need lil help

Originally Posted by Delstateprogramer
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. ## 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;

}
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;
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. ## 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. ## 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. ## 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. ## Re: New Recursion problem need lil help

Originally Posted by Davidovic
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. ## Re: New Recursion problem need lil help

Originally Posted by Delstateprogramer
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. ## 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.

19. ## 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. ## 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)```

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

Faz (July 2nd, 2010)

22. ## Re: New Recursion problem need lil help

Ok thx ill try that out

23. ## Re: New Recursion problem need lil help

Tried this code and it didnt work

```if(number<10)
{
return number;
}
else
{
}```

24. ## 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;