C Program to Count Number of Digits in Integer Number

In this C program, we're going to Count number of digits in a given positive integer number. For examples: 3476 has 4 digits, 897 has 3 digits and so on. Counting digit in a given number is necessary for solving different problems like checking whether a given number is Armstrong or not

Program to Count Number of Digits


#include<stdio.h>
#include<conio.h>

int main()
{
	 long int number, digitCount=0, numberCopy;
	 clrscr();
	 printf("Enter Number: ");
	 scanf("%ld", &number);
	
	 numberCopy = number;
	
	 while(number!=0)
	 {
		  digitCount++;
		  number = number/10;
	 }
	 printf("%ld has %ld digits.", numberCopy, digitCount);
	
	 getch();
	 return(0);
}

Output of the program :

Run 1:
----------
Enter number: 67430 ↲
67430 has 5 digits.

Run 1:
----------
Enter number: 123 ↲
123 has 3 digits.

Run 1:
----------
Enter number: 29 ↲
29 has 2 digits.

Note: ↲ indicates ENTER is pressed.