Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C program to Count the digits of a number

  • Last Updated : 29 Apr, 2020

Given a number N, write a C program to find the count of digits in number N.


We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!

Input: N = 12345
Output: 5
The count of digit in 12345 = 5.

Input: N = 23451452
Output: 8
The count of digits in 23451452 = 8.

Approach: Count of digits in a number can be found efficiently in few steps:

  1. Remove the last digit of number by dividing it with 10.
  2. Increment the count of digit by 1.
  3. Keep repeating steps 1 and 2 until the value of N becomes 0. In this case, there will be no more digit left in the number to count


// C Program to find count of
// digits in a number
#include <stdio.h>
// Find the count of digits
int findCount(int n)
    int count = 0;
    // Remove last digit from number
    // till number is 0
    while (n != 0) {
  //Increment count
        n /= 10;
    // return the count of digit
    return count;
// Driver program
int main()
    int n = 98562;
    printf("Count of digits in %d = %d\n",
 n, findCount(n));
    return 0;
Count of digits in 98562 = 5

Time complexity: O(D), where D is the count of digits in number N.
Auxiliary Space complexity: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :