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:
52 comments Page 2 of 6.

N DAMODHAR said:   1 decade ago
Can anyone explain this the program.

I don't understand the step in s = d+sumdig(n);

Megha said:   10 years ago
But in the program, there is no while loop simple if condition is given. Then how it works?

Amisha aggarwal said:   10 years ago
Super @Vivek, your approach is nice.

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?

Trupti said:   9 years ago
Good explanation @Vivek.

Siva said:   9 years ago
Good explanation @Taher Ali.

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?

Poongodi said:   9 years ago
How comes the value of s=3+2+1,6?

Please explain, & clear it.

Shahid said:   9 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).

Shahid said:   9 years ago
@Taher Ali.

Awesome solution. It works for (- and *).


Post your comments here:

Your comments will be displayed after verification.