A
ali
Hi,
I'm trying to work on a recursive function that will give me root
valuefor a given number.
What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.
I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.
Here is the code:
int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(function(n/10)));
}
}
Will appreciate some help on this,
Thanks,
Ali
I'm trying to work on a recursive function that will give me root
valuefor a given number.
What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.
I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.
Here is the code:
int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(function(n/10)));
}
}
Will appreciate some help on this,
Thanks,
Ali