C Program to Find Total Number of Digit in a Given Number

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

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, tim=0;
    printf("Enter any number: ");
    scanf("%d", &num);
    while(num>0)
    {
        tim++;
        num = num/10;
    }
    printf("\nTotal number of Digit = %d", tim);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the output you will also get on your output screen. 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 total of 5 digits, therefore here is the output you will get:

calculate number of digit in given number c

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

  • Receive any number as input
  • Create a while loop that runs until the number holds the value that is greater than 0
  • Inside the while loop, increment a variable say tim that is responsible to count the total number of digit present inside the given number
  • Never forgot to initialize the variable tim with 0 at start of the program inside the main() function
  • After incrementing the value of the variable tim inside the while loop, divide the given number with 10 for next check and increment
  • In this way, after exiting from the while loop, the variable tim holds the value that will be the value of total number of digit present in given number

C Online Test


« Previous Program Next Program »

Follow/Like Us on Facebook




Subscribe Us on YouTube