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:
52 comments Page 3 of 6.
Chandu said:
9 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.
Hakesh said:
8 years ago
What to do with return 0 statement?
Deepak Johnson said:
7 years ago
Why the value of b is 6?
Samba sivarao said:
6 years ago
@Deepak and @Debjani.
As we are calling the same function with same parameter in the main.
That value is assigned to 'b'.
So same operation takes place as 'a'.
And then b also get 6.
As we are calling the same function with same parameter in the main.
That value is assigned to 'b'.
So same operation takes place as 'a'.
And then b also get 6.
Samba sivarao said:
6 years ago
@Hakesh.
Return 0 belongs to else part of the function.
If n=0 then value 0 is returned .
In the program, the value n is not equal to 0.
So, if the condition is executed and there is no work with else part.
So the value of s is returned.
Return 0 belongs to else part of the function.
If n=0 then value 0 is returned .
In the program, the value n is not equal to 0.
So, if the condition is executed and there is no work with else part.
So the value of s is returned.
Yash Patel said:
4 years ago
@Vivek.
You explained it in a best way. Thank you very much.
You explained it in a best way. Thank you very much.
Daud chacha wangwi said:
1 month ago
Thanks for explaining the answer.
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
Bagesh kumar singh said:
2 decades ago
a=sumdig(123)
n=123 which n!= 0, So n will enter into the loop.
Now in the loop the values of d, n, s are 3(123%10), 12(123/10), 3+sumdig(12) [which will recurcive call].
So finally we will get the value for s is 6(3+2+1).
a=6;
Same as we find the value of b. b=6;
printf("%d%d",a,b); will print 6 and 6.
n=123 which n!= 0, So n will enter into the loop.
Now in the loop the values of d, n, s are 3(123%10), 12(123/10), 3+sumdig(12) [which will recurcive call].
So finally we will get the value for s is 6(3+2+1).
a=6;
Same as we find the value of b. b=6;
printf("%d%d",a,b); will print 6 and 6.
Bagesh kumar bagi said:
2 decades ago
Both function works as same so the output is 6 and 6..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers