C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int sumdig(int);
int main()
{
    int a, b;
    a = sumdig(123);
    b = sumdig(123);
    printf("%d, %d\n", a, b);
    return 0;
}
int sumdig(int n)
{
    int s, d;
    if(n!=0)
    {
        d = n%10;
        n = n/10;
        s = d+sumdig(n);
    }
    else
        return 0;
    return s;
}
4, 4
3, 3
6, 6
12, 12
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
51 comments Page 6 of 6.

Naik said:   2 decades ago
Eg:
sumdig(123)

n=123 which != 0,

So n will enter into the loop.now in the loop the values of d,n,s are 3(123%10),12(123/10),3+sumdig(12)[which will recurcive call.

So finally we will get the value for s is 6(3+2+1)


Post your comments here:

Your comments will be displayed after verification.