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

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

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?

Teja said:   1 decade ago
Hi
See this code
if(n!=0)
{
1. d = n%10;
2. n = n/10;
3. s = d+sumdig(n);
}

In 1. the value of d became 3 //remainder
In 2. the value of n became 12 //quotient
In 3. the value of s became 3+0 //initially sumdig(n)=0
Again
In 1. the value of d became 2
In 1. the value of d became 1
In 2. the value of n became 0 // 1/10=0
In 3. the value of s became 1+5
The condition false the control come to end of function

Madhu said:   1 decade ago
@Teja. You make a small mistake.

In first executing loop you have written that sumdig (n) =0;.

Why it is zero. ?

It is not zero. It will execute sumdig (n) function again.

Shalom chauhan said:   1 decade ago
step1:-> call to function sumdig with parameter 123,now n=123
step2:-> function starts
step3:-> declaration of s & d and contains null value
step4:-> 123!=0 condition true, loop starts
step5:-> d=123%10, d=3
step6:-> n=123/10, n=12
step7:-> s=d+sumdig(12),call to function sumdig with parameter 12, n=12 & s=3+sumdig(12)
step8:->step1 with value of n=12
step8:->finally s=3+2+1,6
step9:->now (n!=0) condition false come out of loop and return value 6
step10:->same execution for b=sumdig(123)
by shalom chauhan
(1)

Satyanarayana said:   1 decade ago
Very nice vivek.

Kantasiva said:   1 decade ago
Here in this program this function (sumdig(123)) will be continue .it is nothing but the recursion function here we are using .
So
if(n!=0)
{
here n=123, 2nd loop n=12,3rd loop n=1, 4th loop =0 then the condition break;
s=d+sumdig(n) , lst time .3+2,5+1=6;

Piyush Sinha said:   1 decade ago
Actually u people are missing a small thing... the output is 3+2+1+0. as the return 0 is in else part which comes when if(n!=0) becomes false and then it return s. I hope everything will be clear now. :)


Post your comments here:

Your comments will be displayed after verification.