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.
Megha said:
9 years ago
But in the program, there is no while loop simple if condition is given. Then how it works?
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.
N DAMODHAR said:
9 years ago
Can anyone explain this the program.
I don't understand the step in s = d+sumdig(n);
I don't understand the step in s = d+sumdig(n);
AKki said:
1 decade ago
You are wrong yash we can't use two return statemt in a common statement.
Arck said:
9 years ago
If I take 1234 as an input I want 234 as an output. How can I do it?
Divya gawande said:
7 years ago
Why in output 6, 6 is two times taken? Please explain in detail.
(2)
Poongodi said:
9 years ago
How comes the value of s=3+2+1,6?
Please explain, & clear it.
Please explain, & clear it.
Yash Patel said:
3 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.
Shahid said:
8 years ago
@Taher Ali.
Awesome solution. It works for (- and *).
Awesome solution. It works for (- and *).
Bagesh kumar bagi said:
1 decade 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