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 4 of 6.

Sowmya said:   1 decade ago
@Hagesh

Can you explain this in clear way?

Sai said:   1 decade ago
#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;
}

Can anyone pls give the flow of control of this program?

Sandeep said:   1 decade ago
Vivek explained nice.

Pratik said:   1 decade ago
One thng is that why it is returning zero?

Can we two retun statement in a common function defination?

Please give me some sugetion to make my "c" strong?

Yash said:   1 decade ago
Yes we can use two or more return statements in a common function but if there are no conditions then it returns threw the first return statement. The others do not execute.

AKki said:   1 decade ago
You are wrong yash we can't use two return statemt in a common statement.

Rahiman said:   1 decade ago
sumdig(123) -->returns the sum of the individual digits of a number( here 123 )

The Logic here is simply taking each and every digit at a time and adding of all those digits.

i) first of all modulo division of a number gives remainder(i.e., 3) and normal division is storing in a same 'n'

Repeating the above process until we get zero in a while loop condition.

Aakanksha said:   1 decade ago
Can anybody give me full execution of this program ?

Vamsy Krishna said:   1 decade ago
I have executed the program and the answer is correct, but I can't understand why the auto variable 's' doesn't take garbage values!!!

Can anyone help me out??

Shrenu said:   1 decade ago
What is the value of 1%10 and 1/10. Can anyone explain this please. I can't understand this step?


Post your comments here:

Your comments will be displayed after verification.