In this blog post, you learn how to write a C program to count number of digits in a number?. We will write a C program to input a number from the user and count number of digits in the given integer using a loop. How to find total digits in a given integer using loop in C programming? Write a C program to count digits in a given integer without using a loop. Also, we will see how to count the number of digits using a recursive function (recursion).
Input Input num: 240627 Output Number of digits: 6
The following are some ways to find the number of digits in an integer in C:
For the below methods, we are considering only positive integers. Also, we have used an “int” type specifier, you can change it according to your requirement. If you are expecting any negative number, you can use the abs() library function before using any mentioned method.
Method-1: Repeated Divide:
In this method, we will take a value from the user’s and continuously divide it by 10 until it becomes zero. During each iteration, we will increase the count variable (initially value 0), which will keep track of the number of digits of a given number.
Algorithm to count the number of digits in a number:
1. Ask the user to enter an integer number. Suppose n = 12345, where n is an integer variable.
int n = 240627;
2. Initialize another variable to store total digits say count = 0.
3. If num > 0 then increment count by 1 i.e. count++.
4. Divide num by 10 to remove the last digit of the given number i.e. num = num / 10.
5. Repeat step 3 to 4 till num > 0 or num != 0.
C Program to count total digits in a given integer using a loop:
#include <stdio.h> int main() { long long num; int count = 0; printf("Enter any number: "); scanf("%lld", &num); //Run loop till num > 0 do { //Increment digit count count++; //Remove last digit of num num /= 10; } while(num != 0); printf("Digits count = %d", count); return 0; }
Output:
Enter any number: 1234
Digits count = 4
Code Analysis:
- After the first iteration, the value of num will be 1234 and the count is incremented to 1.
- After the second iteration, the value of num will be 123 and the count is incremented to 2.
- After the third iteration, the value of num will be 12 and the count is incremented to 3.
- After the fourth iteration, the value of num will be 1 and the count is incremented to 4.
- At the start of five iterations, the value of num will be 0 and the loop is terminated.
Method-2: Logarithmic Approach:
We can use log10(logarithm of base 10) to count the number of digits of positive numbers (logarithm is not defined for negative numbers). Where log10() is a predefined function present in math.h header file. The total number of digits in a given integer is equal to log10(num) + 1.
Count number of digits in an integer without using the loop:
#include <stdio.h> #include <math.h> int main() { int num; int count = 0; //Get input number from user printf("Enter any number: "); scanf("%d", &num); //check number should be positive if(num > 0) { //Calculate total digits count = (num == 0) ? 1 : (log10(num) + 1); printf("Total digits: %d", count); } else { printf("Enter positive number\n"); } return 0; }
Output:
Enter any number: 12345
Digits count = 5
Method-3 Dividing With Powers of Two:
If you know the range of your number, then you can reduce the comparisons. In this method, you need to divide the number by powers of two (e.g. 1, 2, 4, 8, etc.).
#include <stdio.h> int countDigit(unsigned int num) { int count = 1; if (num >= 100000000) { count += 8; num /= 100000000; } if (num >= 10000) { count += 4; num /= 10000; } if (num >= 100) { count += 2; num /= 100; } if (num >= 10) { count += 1; } return count; } int main() { unsigned int num, digits; printf("Enter number = "); scanf("%u", &num); //call function to count digit digits = countDigit(num); printf("\nNumber of digits : %u\n",digits); return 0; }
Output:
Method-4 Repeated Multiplication:
You need to create a temporary variable (temp) and initialized it to 1. Now continuously multiply the temporary variable by 10 until it becomes greater than our number. During each iteration increase the count variable, which will keep track of the number’s length.
#include <stdio.h> int countDigit(unsigned int num) { unsigned int count = 0; unsigned int temp = 1; while (temp <= num) { count++; temp *= 10; } return count; } int main() { unsigned int num, digits; printf("Enter number = "); scanf("%u", &num); //call function to count digit digits = countDigit(num); printf("\nNumber of digits : %u\n",digits); return 0; }
Method-5 Count number of digits using the recursive function:
Using precision you can also calculate the number of digits of a given number. Below I am writing two C codes to find the length of an integer number.
Method 1 Without static variable:
#include <stdio.h> int countDigit(int n) { if (n == 0) { return 0; } return 1 + countDigit(n / 10); } int main() { int num, digits; printf("Enter number = "); scanf("%d", &num); //call recursive function digits = countDigit(num); printf("Number of digits : %d",digits); return 0; }
Output:
Enter any number: 12345
Digits count = 5
Method 2: With static variable:
#include <stdio.h> int countDigit(int n) { static int Count =0; if(n > 0) { Count = Count + 1; countDigit (n / 10); } return Count; } int main() { int num, digits; printf("Enter number = "); scanf("%d", &num); //call recursive function digits = countDigit(num); printf("Number of digits : %d",digits); return 0; }
Output:
Enter any number: 12345
Digits count = 5
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- C Program to swap two nibbles in a byte
- C Program to find first and last digit of a given number
- C program to find the sum of digits of a number
- C Program to find the product of digits of a number
- C program to find sum of first and last digit of a Number
- C Program to swap first and last digit of a number
- C Program to reverse the digits of a number.