whicked.. yea i think your right! thanks Elysia im defiantly gonna get into coding something else that is more clearly adaptable to a recursive function! thanks or your help people!
Printable View
whicked.. yea i think your right! thanks Elysia im defiantly gonna get into coding something else that is more clearly adaptable to a recursive function! thanks or your help people!
If you want a recursive function to calculate the number of digits in a number:
Always start with the base case and make that work. Then write the recursive case.Code:int digits(unsigned int n)
{
// First, the base case. Any number less than 10 is always
// going to have 1 digit exactly. (Even 0 has 1 digit)
if (n < 10)
return 1;
// if the number if 10 or greater then it has more digits.
// Dividing by 10 will remove the rightmost digit which
// we then add 1 to the result for
else
return digits(n/10) + 1;
}