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 1 of 6.
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)
Vivek 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)
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)
(1)
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
Sai said:
1 decade ago
#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;
}
Can anyone pls give the flow of control of this program?
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;
}
Can anyone pls give the flow of control of this program?
Rahiman said:
1 decade ago
sumdig(123) -->returns the sum of the individual digits of a number( here 123 )
The Logic here is simply taking each and every digit at a time and adding of all those digits.
i) first of all modulo division of a number gives remainder(i.e., 3) and normal division is storing in a same 'n'
Repeating the above process until we get zero in a while loop condition.
The Logic here is simply taking each and every digit at a time and adding of all those digits.
i) first of all modulo division of a number gives remainder(i.e., 3) and normal division is storing in a same 'n'
Repeating the above process until we get zero in a while loop condition.
Taher Ali said:
1 decade ago
Initially n,d contains garbage value,
step-1-> N(start)=123 D=3 N=12 S=3+step2
step-2-> N(start)=12 D=2 N=1 S=2+step3
step-3-> N(start)=1 D=1 N=0 s=1+step4
step-4-> If condition is violated return 0.
So, while returning.
step-4=0
s=1+step4 becomes 1 which is step3
s=2+step3 becomes 3 which is step2
s=3+step2 becomes 6 which is returned to main.
step-1-> N(start)=123 D=3 N=12 S=3+step2
step-2-> N(start)=12 D=2 N=1 S=2+step3
step-3-> N(start)=1 D=1 N=0 s=1+step4
step-4-> If condition is violated return 0.
So, while returning.
step-4=0
s=1+step4 becomes 1 which is step3
s=2+step3 becomes 3 which is step2
s=3+step2 becomes 6 which is returned to main.
Bagesh kumar singh said:
1 decade 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.
Siribujje@gmail.com said:
1 decade ago
See @Vijaya. There are two return statements..but one for else..that means,
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;
}
And make sure that its if condition not while loop.
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;
}
And make sure that its if condition not while loop.
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;
Chatrapathi said:
1 decade ago
First :
a = sumdig(123)==>> sumdigt function.
d = n%10==> remainder 3 i.e d=3.
Then it go's to n= n/10==> n= 12.
s=d+sumdigit()==> sumdigit again called does the same here.
n=12 now.
d=n%10 => d=2.
n= n/10 => n=1.
Finally s=3+2+1=6.
a = sumdig(123)==>> sumdigt function.
d = n%10==> remainder 3 i.e d=3.
Then it go's to n= n/10==> n= 12.
s=d+sumdigit()==> sumdigit again called does the same here.
n=12 now.
d=n%10 => d=2.
n= n/10 => n=1.
Finally s=3+2+1=6.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers