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;
}
Discussion:
51 comments Page 4 of 6.
8246 said:
1 decade ago
First d=3 and n=12.
Next d=2 and n=1.
Next d=1 and n=0.
Therefore answer is 3+2+1=6.
Next d=2 and n=1.
Next d=1 and n=0.
Therefore answer is 3+2+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. :)
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;
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;
Satyanarayana said:
1 decade ago
Very nice vivek.
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
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)
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.
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.
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
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
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?
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??
Can anyone help me out??
Aakanksha said:
1 decade ago
Can anybody give me full execution of this program ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers