Wrong output from a recursive function to calculate the sum of the digits of a number

I tried to write a function that would calculate the sum of the digits of a number using recursion, but the result is incorrect. Here is the code:

/*Write a function to calculate sum of digits of a number using recursion*/ /*Author:Udit Gupta Date:10/08/2011*/ #include<stdio.h> int sum (int); int main () { int n,s; printf ("Enter the number:"); scanf ("%d",&n); s = sum (n); printf ("The sum of the digits of the number is %d",s); } int sum (int a) { int f; if (a == 0) { return f; } f = (a% 10) + sum (a/10); } 

Here are some of the output values:

 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out Enter the number:123 The sum of the digits of the number is 7 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out Enter the number:1234 The sum of the digits of the number is 2919930 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out Enter the number:123456 The sum of the digits of the number is 4620297 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out Enter the number:12345 The sum of the digits of the number is 15 /*Only this one seems correct*/ 

Can someone help me understand why this is not working correctly?

+2
source share
5 answers

Take a look at this recursive function in more detail:

 int sum (int a) { int f; if (a == 0) return f; f = (a% 10) + sum (a/10); } 

As long as you are on the right track, and you have the right idea in general, your actual implementation is a bit wrong. To begin, consider the following lines:

 if (a == 0) return f; 

You have the right idea to stop recursing when a reaches zero, but the way you do it is a bit off. In particular, you return the value of the integer f , but you never initialized it. This means that the return value is completely arbitrary. Instead of writing this, I think you probably wanted to write something closer to

 if (a == 0) return 0; 

which correctly says "if the number is zero, the sum of its digits is zero."

Similarly, look at the last line of your function:

 f = (a% 10) + sum (a/10); 

Again, your intuition is spot-on: the sum of the digits of a number is determined by the sum of its first digit and the sum of its remaining digits. However, note that while you are correctly calculating the sum of the digits, you are not returning the sum of the digits correctly. In fact, you do not return anything if you execute this code, so the return value from the function is not specified, therefore, garbage output. To fix this, consider rewriting the code as follows:

 return (a % 10) + sum (a / 10); 

This actually says to return the value you just created here, instead of storing it in a local variable that will be immediately cleared as soon as the function returns.

I believe that the reason you encoded this function is because you are under the impression that the value of int f; carried over through function calls. Unfortunately, this is not so. When writing a recursive function, each instance of the function is completely independent of each other, and local variables available in one recursive call are not available in other recursive calls. Therefore, although each recursive call has its own int f variable, these variables are completely independent of each other. The value is not carried through them. If you want to pass values ​​over recursive functions, the best way to do this is to use the return value of the recursive calls, or (if necessary) by passing the pointer down some value down through recursion.

Hope this helps!

+5
source

When a is 0, you return an uninitialized value (f was not initialized).

Change it to:

 if (a == 0) return 0; 

You also forgot the return at the end of the function:

 return (a% 10) + sum (a/10); 

It is strongly recommended that you always compile the -Wall flag, which alerts you to these errors.

+4
source

Your recursive function does not evaluate anything, either returns an uninitialized int or nothing. You need to return the work you perform in the function.

 int sum (int a) { if (a == 0) { return 0; } return (a% 10) + sum(a/10); } 
+2
source
 return a == 0 ? 0 : ((a% 10) + sum (a/10)); 
+1
source

You return only f, it is 0, but not if it is not, which makes your return value undefined. I assume you want:

 int sum (int a) { int f; if (a == 0) return 0; f = (a % 10) + sum (a / 10); return f; } 
0
source

All Articles