c - Error in calculation of the number of digits in a number -


write program using if statement calculates how many digits number contains:

my code :

#include <stdio.h> int main (void) { int n; printf ("enter number :"); scanf ("%d",&n); if  ( n<=9) printf ("textnumber has 1 digit:"); if ( n <=99) printf ("textnumber has 2 digits:"); if (n<=999) printf ("textnumber has 3 digits:"); if (n <=9999) printf ("textnumber has 4 digits:");  return 0; } 

the problem when run this, , put example : 223

i have result in screen :

textnumber has 3 digitstextnumberhasfourdigits... 

where wrong?

this because compiler punishing not indenting code properly.

just kidding, in fact, your logic flawed (and/or expectation doesn't match working of code): need else ifs because if number greater limit, it greater smaller limits too, of them printed - erroneously.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -