public static int NumberOfDigits(int n)
{
int counter = 0;
while (n > 0)
{
n = n / 10;
counter++;
}
return counter;
}
How it works:
For any given number, say 6234 the loop runs till the number is a 0.
We iteratively divide the number by 10 and assign the quotient to itself. So
n = 6234 / 10 = 623
n = 623 / 10 = 62
n = 62 / 10 = 6
n = 6 / 10 = 0
We get the counter which will be set to 4 at the end of loop.