The Program in C that counts the number of digits in a given number is given below:
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
int count = (int)log10(n) + 1;
printf("Number of digits: %d", count);
return 0;
}
Output:
Enter a number: 9999999999
Number of digits: 10
Pro-Tips💡
In this program, the log10 function returns the base 10 logarithm of the given number.
Since log10 of a number n gives the number of digits in n – 1, we add 1 to it to get the number of digits. The final result is casted to int as the log10 function returns a double.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.