Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6253655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:02:31+00:00 2026-05-24T14:02:31+00:00

I was trying to write a function that would compute the sum of the

  • 0

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:

/*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 figure out why this isn’t working correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:02:31+00:00Added an answer on May 24, 2026 at 2:02 pm

    Let’s 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);
    }
    

    While you’re on the right track and you have the right idea in general, your actual implementation is a bit buggy. For starters, let’s look at these lines:

    if (a == 0)
        return f;
    

    You have the right idea to terminate the recursion when a reaches zero, but the way you’re doing it is a bit off. In particular, you’re returning the value of the integer f, but you’ve never initialized it. This means that the return value is completely arbitrary. Instead of writing this, I think that you probably meant 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, take a 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 are given by the sum of its first digit and the sum of the rest of its digits. However, notice that while you’re correctly computing the sum of the digits, you aren’t correctly returning the sum of the digits. In fact, you don’t return anything at all if you execute this code, so the return value from the function is unspecified, hence the garbage output. To fix this, consider rewriting the code like this:

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

    This actually says to hand back the value that you just generated right here, instead of storing it in a local variable that will be immediately cleaned up as soon as the function returns.

    I believe that the reason you coded this function this way is that you’re under the impression that the value of int f; is carried across the function calls. Unfortunately, it is not. When writing a recursive function, each instance of the function is completely independent of each other instance and local variables accessible in one recursive call are not accessible in other recursive calls. Consequently, even though each recursive call has its own variable int f, those variables are all completely independent of one another. The value isn’t carried through them. If you want to communicate values across recursive functions, the best way to do it is by using the return value of the recursive calls, or (if you must) by passing a pointer to some value down through the recursion.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find/write a function that would perform the same operation as imlincomb
I am trying to write a general function in F# that would return all
I'm trying to write a function that would give me an array of days
I'm trying to write a function, that would use native openssl to do some
I'm trying to write a function that accepts a variable number of parameters like
All, I'm trying to write a function that would check in my users table
I'm trying to write a "blinds" function that would close a DIV in a
I'm trying to write a simple function in C that would calculate the difference
I am trying to write a function that executes immediately but can also be
I am trying to write a small PHP function that would go through a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.