C Program to Count Number of Digits in an Integer - Source Code, Output & Execution Flow

C Program to Count Number of Digits in an Integer

This tutorial explains c program to count number of digits in an integer with C source code, sample output, and step-by-step execution flow.

#include <stdio.h>
int main() {
    int n, count = 0;
    printf("Enter an integer: ");
    scanf("%d", &n);
    if (n == 0) count = 1;
    while (n != 0) { n /= 10; count++; }
    printf("Number of digits = %d
", count);
    return 0;
}
Enter an integer: 12345
Number of digits = 5

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.