C Program to Count Digits of a Number

To Count the number of Digits in an Integer, we divide the integer by 10 and assign the result to integer variable and increase the counter by 1. We continue this procedure until the result becomes zero (0) to get the final count of Digits in an Integer.

C program to Count number of Digits in an Integer using for loop


#include<stdio.h>

int main()
{ 
	int original,r,number,count=0;

	printf("\n Enter any number:- ");
	scanf("%d",&number);
 
	original = number;
 
	for(i = num; i > 0; i = i/10)
	{
	++count; 
	}
 
	printf("\n There are %d digits in %d  \n \n",count,original);

	return 0;
}

/* Output of above code:

 Enter any number:- 1234

 There are 4 digits in 1234

*/

C program to Count number of Digits in an Integer using while loop

/* Aim: Write a program to accept an integer and count the number of digits in the number */

#include<stdio.h>

int main()
{ 
	int original,r,num,count=0;

	printf("\n Enter any number:- ");
	scanf("%d",&num);
 
	original=num;
 
	while(num>0)
	{
	num/=10;
	++count; 
	}
 
	printf("\n There are %d digits in %d  \n \n",count,original);

	return 0;
}

/* Output of above code:

 Enter any number:- 9856

 There are 4 digits in 9856 

*/

C program to Count number of Digits without using loop


#include<stdio.h>
#include<math.h>

int main()
{
    int number;
    int digits_count = 0;

    printf("\n Enter any number:- ");
    scanf("%d", &number);

    digits_count = log10(num) + 1;

    printf("\n There are %d Digits present in entered number. \n", digits_
count);

    return 0;
}