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 262 views
1 answer 190 views
1 answer 203 views
1 answer 155 views
155 views asked Oct 7, 2015 by avibootz
2 answers 248 views
2 answers 238 views
1 answer 128 views
...