Sum Digits using Recursion problem

My instructions are:
Create program definition for sumDigits Function. Function will receive and return an integer.
The Main method will: ask for numerous non-negative integers, pass integer into recursive function called sumDigits, receive result back from sumDigits and output to screen, and include system("Pause");
The sumDigits Function: Recursively call sumDigits adding up the integers received, return the result to the main method for output, and if a single digit is received, just return it to the main method.
The output will look like:
Enter a nonnegative integer: 1234
The sum of the digits of 1234 is 10


Here is what I have. It compiles, but the program stops. I am sure that I am missing something in the sumDigits function, but can't seem to find my problem. Can someone point me in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# include <iostream>

using namespace std;
int n = 1, sum = 0;

int sumDigits(int& n, int sum) //sumDigits function
{    
    if (n%100 <= 0) // call to stop recursion
    {
        return sumDigits (n, sum);
    }
    else
    {
        sum = sum + n%10;  // recursion to add digits
        return sumDigits(n, sum); // returning sum for print
    }
}

int main()
{
	n = 1, sum = 0;
	while (n > 0)
	{
          cout << "Enter a non-negative integer (enter 0 to end): \n";
          cin >> n;
          sumDigits (n, sum); // calling sumDigits
          cout << "The sum of all digits ", n, " is: ", sum, "\n"; 
    }
     
	return 0;
	system ("Pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>

using namespace std;
int n = 1, sum = 0;

int sumDigits(int n, int sum) //sumDigits function
{    
	if (n== 0) // call to stop recursion
    {
        return sum;
    }
    else
    {
        sum = sum + n%10;  // recursion to add digits
		n= n/10;
        return sumDigits(n, sum); // returning sum for print
    }
}

int main(int argc, char* argv[])
{
	n = 1, sum = 0;

        cout << "Enter a non-negative integer (enter 0 to end): \n";
        cin >> n;
        sum = sumDigits (n, sum); // calling sumDigits
        cout << "The sum of all digits "<< n << " is: " << sum << "\n"; 
  
	system ("Pause");
        return 0;
}


You didnt need the while. Try that.
Last edited on
The problem is n is not being changed between subsequent calls to sumDigits. Say you pass in 5, sumDigits would go to the else case, add 5 to the sum then it would call sumDigits with 5 and the increased sum. Then, sumDigits would go to the else case, add 5 to the sum then it would call sumDigits with 5 and the increased sum. This is an infinite loop. You need to modify n before the next recursive call (hint: decimal point needs to be moved over by one).

if (n%100 <= 0)
This base case condition is wrong. If n == 100, then sumDigits would (theoretically) exit and the sum would not be 1. Speaking of the base case, you're calling sumDigits (with the same parameters) again. This leads to an infinite loop. The base case needs to simply do nothing. You can make an if statement such that if there is at least one digit left to work with, then it does your recursive step (and then no need for an else because the else would be your base case that does nothing).

int sumDigits(int& n, int sum)
n shouldn't be passed by reference in this case. The print statement on the other end will be wrong.

1
2
return 0;
system ("Pause");

Here, system("pause") is unreachable (return 0; terminates the program). It should be placed before return 0;. Also, there are much better ways to achieve the same effect without using system. I don't know why it's a requirement in this assignment.

http://www.cplusplus.com/forum/beginner/1988/
.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <conio.h>
using namespace std;
int sumDigits(int& n, int& sum);

int main()
{
    int n, sum;

        cout << "Enter a non-negative integer (enter 0 to end): \n";
        cin >> n;
        if (n == 0)
           {return 0;}
        sumDigits(n , sum);
        cout << "The sum of all digits " << n << " is: " << sum << "\n\n\n";
        main();
        return 0;
}

int sumDigits(int& n, int& sum)
{
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
num1 = n / 1000000000;
num2 = n % 1000000000 / 100000000;
num3 = n % 1000000000 % 100000000 / 10000000;
num4 = n % 1000000000 % 100000000 % 10000000 / 1000000;
num5 = n % 1000000000 % 100000000 % 10000000 % 1000000 / 100000;
num6 = n % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 / 10000;
num7 = n % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 / 1000;
num8 = n % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 / 100;
num9 = n % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 / 10;
num10 =  n % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 % 10 / 1;

sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
return (sum);
}

:] hope i helped. correct me if i am wrong. thanks. :]
nethinkz thats not a recursive function ;)
what is a recursive function sir?

Recursion

http://www.cplusplus.com/forum/articles/2231/

COMPUT: a programming technique where a routine performs its task by delegating part of it to another instance of itself.
I still don't get it. lol. I have been studying programming for 1 month in school. We have only tackled pseudocode, flowchart and currently turbo c. I am doing an advance study for c++. I still suck. lol.
Be patience. You wont learn everything in 1 month :P
1
2
3
4
5
6
7
8
9
10
11
12

int factorial(int n)
{
   if(n == 1)
   {
      return 1;
   }
   else
   {
      return n * factorial(n - 1);  //This is n * (n - 1) * (n - 1) ....
   }
}



As you can see this function calls itself
Example.

N = 5

the value this function returns is 5*4*3*2*1


Last edited on
Thank you all ... I think I figured out my mistake.
haha. I still have 4 years to study programming. :]
I saw the debate at http://www.cplusplus.com/forum/articles/2231/
sooooo... is it just like a loop? lol. :)

1
2
3
4
5
6
{
    int n, ctr = 1;
    while (ctr <= n)
       {n = n*ctr;
       ctr++;}
}

does it give the same result? x_X
Nope is not the same result.

trace it, example:

Considering n = 5
Sum all values = 5

1
2
3
4
5
6
{
    int n, ctr = 1;
    while (ctr <= n)
       {n = n*ctr;
       ctr++;}
}


if n = 5 it would be 5*1*2*3*4*5 instead of 5

If you mean the factorial yes.

Its like a "loop".

In some cases it would be faster to do it with recursion and others with loops
Last edited on
Topic archived. No new replies allowed.