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
Methods to Count Digits of a Number
There are a few methods to count the digits of a number mentioned below:
- Using Loops
- Logarithmic Approach
- Using Recursion
- Using Repeated Multiplication
- By Dividing with Powers of Two
1. Using Loops
The count of digits in a number can be found efficiently in a few steps:
- Remove the last digit of the number by dividing it by 10.
- Increment the count of digits by 1.
- Keep repeating steps 1 and 2 until the value of N becomes 0. In this case, there will be no more digits left in the number to count.
Example:
// C Program to Find Count of
// Digits in a Number
// Using Loops
#include <stdio.h>
// Find the count of digits
int findCount(int n)
{
// For digit 0
if(n == 0)
return 1;
int count = 0;
// Remove last digit from number
// till number is 0
while (n != 0) {
// Increment count
count++;
n /= 10;
}
// return the count of digit
return count;
}
// Driver program
int main()
{
int n = 98562;
printf("Count of digits in %d = %d\n", n, findCount(n));
return 0;
}
// This code is modified by Susobhan Akhuli
Output
Count of digits in 98562 = 5
The complexity of the above method
Time complexity: O(D), where D is the count of digits in the number N.
Auxiliary Space: O(1)
2. 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
3. Using Recursion
Below is the C program to count the number of digits using functions:
// C Program to Count the Number
// Of digits in a number
// Using Recursion
#include <stdio.h>
int Count_Of_Digits(int n)
{
static int Count = 0;
if (n > 0) {
Count = Count + 1;
Count_Of_Digits(n / 10);
}
return Count;
}
// Driver code
int main()
{
int n = 98562, Count = 0;
Count = Count_Of_Digits(n);
printf("Count of digits in %d = %d\n", n, Count);
return 0;
}
Output
Count of digits in 98562 = 5
4. Using Repeated Multiplication
Below is the C program to count the number of digits in a number using repeated multiplication:
// C Program to Count the Number
// Of Digits in a Number
// Using Repeated Multiplication
#include <stdio.h>
// function to find the count
// the number
int findCount(unsigned int n)
{
unsigned int count = 0;
unsigned int temp = 1;
while (temp <= n) {
count++;
temp *= 10;
}
return count;
}
// Driver code
int main()
{
unsigned int n = 98523, digits;
// call function to count digit
digits = findCount(n);
printf("Count of digits in %d = %d\n", n, digits);
return 0;
}
Output
Count of digits in 98523 = 5
5. By Dividing with Powers of Two
Below is the C program to count the number of digits in a number by dividing with powers of two:
// C Program to Count the Number
// Of Digits in a Number
// By Dividing with Powers of Two
#include <stdio.h>
int findCount(unsigned int n)
{
int count = 1;
if (n >= 100000000) {
count += 8;
n /= 100000000;
}
if (n >= 10000) {
count += 4;
n /= 10000;
}
if (n >= 100) {
count += 2;
n /= 100;
}
if (n >= 10) {
count += 1;
}
return count;
}
// Driver code
int main()
{
unsigned int n = 98532, digits;
// call function to count digit
digits = findCount(n);
printf("Count of digits in %d = %d\n", n, digits);
return 0;
}
Output
Count of digits in 98532 = 5