title: Incorrect output from recursive function to compute sum of digits of a number
body: <p>I was trying to write a function that would compute the sum of the digits of a number using recursion, but the output is incorrect.  Here's the code:</p>
  
  <pre><code>/*Write a function to calculate sum of digits of a  number using recursion*/
  /*Author:Udit Gupta     Date:10/08/2011*/
  
  #include&lt;stdio.h&gt;
  
  int sum (int);
  
  int main () {
      int n,s;
  
      printf ("Enter the number:");
      scanf ("%d",&amp;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);
  }
  </code></pre>
  
  <p>Here are some of the output values:</p>
  
  <pre><code> 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*/
  </code></pre>
  
  <p>Can someone help me figure out why this isn't working correctly?</p>