Logarithmic Approach

Below is the C program to count the number of digits in a number without using a loop:

C
// 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

Similar Reads

Methods to Count Digits of a Number

There are a few methods to count the digits of a number mentioned below:...

2. Logarithmic Approach

Below is the C program to count the number of digits in a number without using a loop:...

3. Using Recursion

Below is the C program to count the number of digits using functions:...