Counting digits in an integer is a common programming problem. For instance, if you take 12345, it'll show you it has 5 digits. This task helps when you're processing numerical data.
Prerequisites for Counting Digits in C
Before writing the C program, ensure you know these:
<stdio.h>for input and output functions.- The
whileloop to iterate over the digits. - Integer division using
/to remove the last digit. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Count Number of Digits in an Integer
Here's the C code to count the number of digits in an integer:
#include <stdio.h>
int main() {
int n, count = 0;
// Read an integer from the user
printf("Enter an integer: ");
scanf("%d", &n);
// Continue until n becomes 0
while (n != 0) {
n /= 10;
count++;
}
printf("The number of digits is: %d\n", count);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Count Digits Program
When you run the program with 12345 as input, you'll see:
Enter an integer: 12345
The number of digits is: 5
Algorithm to Count Digits in C
Counting the number of digits requires repeatedly removing the last digit until none are left. This is how the logic unfolds:
- Read the integer
nfrom the user. - Initialize
countto 0. - Repeat while
nis not zero: - Divide
nby 10 to strip the last digit. - Increment
countby 1 for each division. - End repeat.
flowchart TD
A([Start]) --> B[/Read n/]
B --> C[count = 0]
C --> D{"n != 0?"}
D -->|Yes| E["n = n / 10"]
E --> F["count = count + 1"]
F --> D
D -->|No| G([End])How the Count Digits Program Works
Let's go through the code line by line:
#include <stdio.h>: Includes the Standard Input Output library for functions likeprintfandscanf.- The
main()function: Entry point of the program. - Declares
int n, count = 0;: Initializescountto store the number of digits. printf("Enter an integer: "): Prompts the user to enter a number.scanf("%d", &n): Reads the integer input from the user.- The
while (n != 0)loop: Continues as long asnis not zero. n /= 10;: Removes the last digit ofn.count++;: Increments the digit count for every digit removed.printf("The number of digits is: %d\n", count);: Displays the final count.return 0;: Ends the program successfully.
Quick Quiz — test your understanding
Tap an option to check your answerWhat loop is suitable for counting digits in an integer?
Time and Space Complexity of the Count Digits Program
Analyzing the performance of this program involves understanding both time and space aspects.
Time Complexity: O(log n). The symbol n here represents the actual integer input length — for a number like 12345, n is 5. The loop while (n != 0) divides n by 10 each time, reducing its size logarithmically. So, with 10 digits, about 10 iterations occur.
Space Complexity: O(1). The program uses only a fixed number of integer variables: n and count, making the memory usage constant regardless of the input size.
Common mistake: Forgets to account for negative numbers, leading to wrong counts if not handled separately.
Key takeaway: Dividing by 10 is a simple, effective way to truncate a number, letting you count digits efficiently.
FAQs About Counting Digits in C
Here are some common questions about counting digits in an integer using C.
n hits zero. You might wanna add a check for zero as a special case.n before the loop.int, usually up to 10 digits for 32-bit integers.while loop’s more flexible here since it runs based on a condition, which works great for counting digits until n becomes zero.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Find Factorial of a Number
- C Program to Add Two Integers with Explanation and Output
- C Program to Multiply Two Floating-Point Numbers
- C Program to Compute Quotient and Remainder Easily
- C Program to Generate Multiplication Table with Example
Conclusion
Counting digits by dividing by 10 is a neat trick seen in many coding problems. It's good to practice with negative numbers and zero to cover those edge cases. You’ll find similar logic used for digit extraction in various algorithms.
Ready for more? Test your skills with our C language quiz or run more programs in our C online compiler.