Linked Questions

Popular Questions

Unsigned function to return digits using any numbers

Asked by At

I'm working on an exercise where I have to program an unsigned function in C++ --- The function has returned the number of digits in num and must work on any number.

The issue  --- when I do unsigned num is greater than 10 digits, it still 
shows 10 as the answer.  what am I doing wrong?


unsigned numDigits(unsigned num)  
{
if (num == 0)
return 0; 
return 1 + numDigits(num / 10); 
return (num);
}

int main()
{
unsigned num = 12345678901;
cout << "Number of Digits: " << numDigits(num);
}

Related Questions