C program to Count the digits of a number
Last Updated :
30 May, 2023
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
#include <stdio.h>
int findCount( int n)
{
int count = 0;
while (n != 0) {
count++;
n /= 10;
}
return count;
}
int main()
{
int n = 98562;
printf ( "Count of digits in %d = %d\n" , n, findCount(n));
return 0;
}
|
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
#include <math.h>
#include <stdio.h>
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
#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;
}
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
#include <stdio.h>
int findCount(unsigned int n)
{
unsigned int count = 0;
unsigned int temp = 1;
while (temp <= n) {
count++;
temp *= 10;
}
return count;
}
int main()
{
unsigned int n = 98523, digits;
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
#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;
}
int main()
{
unsigned int n = 98532, digits;
digits = findCount(n);
printf ( "Count of digits in %d = %d\n" , n, digits);
return 0;
}
|
Output
Count of digits in 98532 = 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...