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.
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);
Sudhakar said:
9 years ago
None of Them is correct answer.
According to mine at last step condition will fail and else part will be executed which will return 0, both time.
Answer should be 0, 0.
What others think?
According to mine at last step condition will fail and else part will be executed which will return 0, both time.
Answer should be 0, 0.
What others think?
Arck said:
9 years ago
If I take 1234 as an input I want 234 as an output. How can I do it?
Megha said:
9 years ago
But in the program, there is no while loop simple if condition is given. Then how it works?
Amisha aggarwal said:
9 years ago
Super @Vivek, your approach is nice.
Trupti said:
9 years ago
Good explanation @Vivek.
Siva said:
9 years ago
Good explanation @Taher Ali.
Poongodi said:
9 years ago
How comes the value of s=3+2+1,6?
Please explain, & clear it.
Please explain, & clear it.
Shahid said:
8 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).
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:
8 years ago
@Taher Ali.
Awesome solution. It works for (- and *).
Awesome solution. It works for (- and *).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers