C Program to Find the Total Number of Digits in a Given Number

In this article, we will learn to create a program in C that will ask the user to enter any number (at run-time) to find and print the total number of digits present in that given number. Here is the program:

int main()
    int num, tim=0;
    printf("Enter any number: ");
    scanf("%d", &num);
        num = num/10;
    printf("\nTotal number of Digit = %d", tim);
    return 0;

As the above program was written in the Code::Blocks IDE, here is the output you will also get on your output screen after a successful build and run. This is the first snapshot of the sample run:

c program print total number of digit

Supply any number, say 24304, and press ENTER to see the output. As the number 24304 has a total of 5 digits, here is the output you will get:

calculate number of digit in given number c

Here are some of the main steps used in the above program:

Before closing the discussion on this post, I'm willing to include one more example that does the same job of finding the total number of digits in a given number. I used comments to describe the main codes used in the following C program:

C Code
#include <stdio.h>

int main() {
    int num, count = 0;         // declare variables for the number and count of digits

    printf("Enter a number: "); // ask the user to input a number
    scanf("%d", &num);          // read the input number from the user

    while (num != 0) {          // loop until the number is completely divided
        count++;                // increment the count of digits
        num /= 10;              // divide the number by 10 to remove the last digit

    printf("Total number of digits: %d\n", count);   // print the total number of digits

    return 0;
Enter a number: 987654321
Total number of digits: 9

C Quiz

« Previous Program Next Program »

Liked this post? Share it!