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.

Code Java:

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???

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

Code Java:

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???

Re: New Recursion problem need lil help

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

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

Re: New Recursion problem need lil help

Code Java:

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;
}
}

Re: New Recursion problem need lil help

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

Re: New Recursion problem need lil help

Quote:

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

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

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.

Code :

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

Code :

sum=digit;
number=sum;
adddigits(number);

That code is will do the exact same thing as

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.

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

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.

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.

Re: New Recursion problem need lil help

Quote:

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

Re: New Recursion problem need lil help

Quote:

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?

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.

Re: New Recursion problem need lil help

Quote:

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)

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

Code Execution:

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:

Code Pseudo:

function sumOfDigits(number)
if(number < 10)
return number
else
return number % 10 + sumOfDigits(number / 10)

Re: New Recursion problem need lil help

Re: New Recursion problem need lil help

Tried this code and it didnt work

Code Java:

if(number<10)
{
return number;
}
else
{
return (number%10)+((addDigits(number/10)));
}

Re: New Recursion problem need lil help

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

Code Java:

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;
}

Re: New Recursion problem need lil help

Quote:

addDigits(number);

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

Re: New Recursion problem need lil help

lol the code works and that all that matters