Question: Write a program using if statement which calculates how many digits a number contains:

My code :

```
#include <stdio.h>
int main (void)
{
int n;
printf ("Enter number :");
scanf ("%d",&n);
if  ( n<=9)
printf ("Textnumber has one digit:");
if ( n <=99)
printf ("Textnumber has two digits:");
if (n<=999)
printf ("Textnumber has three digits:");
if (n <=9999)
printf ("Textnumber has four digits:");

return 0;
}

```

The problem is that when I run this, and I put for example : 223 

I have the result in my screen :

```
Textnumber has three digitsTextnumberhasfourdigits...

```

Where am I wrong?

Answer: 
This is because your compiler is punishing you for not indenting your code properly.

Just kidding, in fact, **your logic is flawed** (and/or your expectation doesn't match the working of the code): you need `else if`s because if your number is greater than a certain limit, *it will be greater than smaller limits too,* so all of them will be printed - erroneously.
Just try this code, as [H2CO3](https://stackoverflow.com/users/529758/h2co3) has given the right explanation...

```
#include <stdio.h>
int main (void)
{
int n;
printf ("Enter number :");
scanf ("%d",&n);
if  ( n<=9)
    printf ("Textnumber has one digit:");
else if ( n <=99)
    printf ("Textnumber has two digits:");
else if (n<=999)
    printf ("Textnumber has three digits:");
else if (n <=9999)
    printf ("Textnumber has four digits:");

return 0;
}

```

Try a neater way.

```
int digits=0;
while(num)
{
    num/=10;
    digits++;
}

```