C program to count the digits of a given number

C Program to count the digits of a given number

Suppose you have a a number and you want to know how many digits are there. So don’t worry we’re going to learn how to use the C programming language to count the digits in a number.

In this tutorial you will learn how to write a c program to count the digits of a given number.

Count digits of a given number in C

#include <stdio.h>
int main() {
    int num, count = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    while (num != 0) {
        num /= 10;
        count++;
    }
    printf("Number of digits: %d", count);
    return 0;
}

Output:

Enter a number: 12
Number of digits: 2