How to count input number digits in C

1 Answer

0 votes
#include <stdio.h> 

int countdigits(int n);

int main(void)
{   
     int n;

     printf("Enter a number:  ");
     scanf("%d",&n);
     
     int size = countdigits(n);
     
     printf("%d", size);
         
     return 0;
}

int countdigits(int n)
{
    int count = 0;

    while (n != 0)
    {
        n /= 10;
        count++;
    }
    
    return count;
}

 
/*
run:
   
Enter a number:  12345
5

*/

 



answered Nov 14, 2016 by avibootz
edited Nov 14, 2016 by avibootz

Related questions

1 answer 252 views
1 answer 183 views
1 answer 198 views
1 answer 150 views
150 views asked Oct 7, 2015 by avibootz
2 answers 243 views
2 answers 225 views
1 answer 119 views
...