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 2 of 6.
Srinivas kumar said:
1 decade ago
d%10 gives the last number of a given number i.e., for example for number 123 last number is 3 and n/10 gives the except last number it gives whole number i.e., for example for number last number is 3 and remaining are 12.
Like that you can solve the code.
Like that you can solve the code.
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.
Ammu said:
8 years ago
step 1: d=123%10==3
n=123/10==12
s=3+sumdig(12)since n==12.
step 2: d=12%10==2
n=12/10==1
s=5(since 3+2)+sumdig(1).
step 3: d=1%10==1
n=n/10==0
s=5+1==6
so s==6.
n=123/10==12
s=3+sumdig(12)since n==12.
step 2: d=12%10==2
n=12/10==1
s=5(since 3+2)+sumdig(1).
step 3: d=1%10==1
n=n/10==0
s=5+1==6
so s==6.
(7)
Naik said:
2 decades ago
Eg:
sumdig(123)
n=123 which != 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)
sumdig(123)
n=123 which != 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)
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. :)
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?
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.
Vijaya said:
1 decade ago
If we have two return stmts without any condition then the first return statement is executed the next one is ignored. In the above program why the output 0 and 0 can anyone explain me.
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.
Shubham Singh said:
1 decade ago
Can anyone tell me that in this program as we know that "if" is executed only once but it will between executed again and again. It is due to sumdig (n) or any other reason.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers