Logarithmic Approach
Below is the C program to count the number of digits in a number without using a loop:
// C Program to Count the Number
// Of Digits in a Number
// Using Logarithmic Approach
#include <math.h>
#include <stdio.h>
// Driver code
int main()
{
int n = 98562;
int count = 0;
count = (n == 0) ? 1 : log10(n) + 1;
printf("Count of digits in %d = %d\n", n, count);
return 0;
}
Output
Count of digits in 98562 = 5
C program to Count the digits of a number
Given a number N, write a C program to find the count of digits in the number N.
Examples:
Input: N = 12345
Output: 5
Explanation: The count of digit in 12345 = 5
Input: N = 23451452
Output: 8
Explanation: The count of digits in 23451452 = 8