Is this program the one that outputs "6 12"?
Code:
#include <stdio.h>
int main()
{
int a,b;
a=sumdig(123);
b=sumdig(123);
printf("%d%d",a,b)
return 0;
}
int sumdig(int n)
{
static int s=0;
int d;
if(n!=0)
{
d=n%10;
n=(n-d)/10;
s=s+d;
return sumdig(n);
}
else return(s);
}
Okay, here's the first call:
Code:
sumdig(123)
d = n % 10; d = 123 % 10; d = 3;
n = (n-d)/10; n = (123-3)/10; n = 120/10; n = 12;
s = s + d; s = 0 + 3; s = 3;
sumdig(12)
d = n % 10; d = 12 % 10; d = 2;
n = (n-d)/10; n = (12-2)/10; n = 10/10; n = 1;
s = s + d; s = 3 + 2; s = 5;
sumdig(1)
d = n % 10; d = 1 % 10; d = 1;
n = (n-d)/10; n = (1-1)/10; n = 0/10; n = 0;
s = s + d; s = 5 + 1; s = 6;
The second call is messed up because s isn't zero.