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

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.

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??

Chandu said:   8 years ago
In the above program, they mention printf only once after performing sumdig functions. Therefore, the program prints the output values of sumdig values only.

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?

Noel said:   7 years ago
I think the sticking point for me and others is once you have d = 1%10.

If it were division, the result would be 0. However, with MODULAR MATH, 1%10 is 1.
(1)

Divya said:   1 decade ago
Can someone explain, when n=0 after n=n/1;if (n!=0) condition becomes false.

So, it should return 0. How can it even reach the statement returns?

Andy said:   1 decade ago
I want to know that at last step where the function is 3+2+1+sumdig(0) , so what is the value of sumdig(0). It's value is 0 how?

Yasin said:   1 decade ago
@ Auto Variable Initialization

S value is initialized with Garbage value.

But, this value is over written with s=d+sumdig(N);

Shahid said:   8 years ago
@Vivek.

The same logic can't be applied for (d*sumdig(n));

Answer for this (0,0) and (d-(sumdig(n));answer for this (2,2).

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.