C# Program to Count Digits in a Number
You can count digits in a number in C# by converting it to a string and using
number.ToString().Length or by repeatedly dividing the number by 10 and counting the steps with a loop.Examples
Input12345
Output5
Input0
Output1
Input-9876
Output4
How to Think About It
To count digits in a number, think about how many times you can divide the number by 10 before it becomes zero. Each division removes the last digit, so counting these divisions tells you how many digits the number has. For negative numbers, ignore the sign and count digits of the absolute value.
Algorithm
1
Get the input number.2
Convert the number to its absolute value to ignore negative sign.3
If the number is zero, return 1 because zero has one digit.4
Initialize a count variable to zero.5
While the number is greater than zero, divide it by 10 and increment the count.6
Return the count as the number of digits.Code
csharp
using System; class Program { static void Main() { int number = -9876; int count = CountDigits(number); Console.WriteLine(count); } static int CountDigits(int num) { num = Math.Abs(num); if (num == 0) return 1; int count = 0; while (num > 0) { num /= 10; count++; } return count; } }
Output
4
Dry Run
Let's trace the number -9876 through the code to count its digits.
1
Start with number
Input number = -9876
2
Convert to absolute
num = 9876
3
Check if zero
num != 0, continue
4
Initialize count
count = 0
5
Loop divide and count
num=9876, count=0 -> num=987, count=1 -> num=98, count=2 -> num=9, count=3 -> num=0, count=4
6
Return count
count = 4
| num | count |
|---|---|
| 9876 | 0 |
| 987 | 1 |
| 98 | 2 |
| 9 | 3 |
| 0 | 4 |
Why This Works
Step 1: Handle negative numbers
Using Math.Abs removes the negative sign so digit count is correct.
Step 2: Count digits by division
Dividing by 10 removes the last digit each time, so counting divisions counts digits.
Step 3: Special case zero
Zero has one digit, so return 1 immediately if input is zero.
Alternative Approaches
String conversion
csharp
using System; class Program { static void Main() { int number = 12345; int count = number.ToString().TrimStart('-').Length; Console.WriteLine(count); } }
This method is simple and uses string length but may be slower for very large numbers.
Recursive division
csharp
using System; class Program { static void Main() { int number = 4567; Console.WriteLine(CountDigits(number)); } static int CountDigits(int num) { num = Math.Abs(num); if (num < 10) return 1; return 1 + CountDigits(num / 10); } }
This uses recursion to count digits but can cause stack overflow for very large numbers.
Complexity: O(d) time, O(1) space
Time Complexity
The loop divides the number by 10 each time, so it runs once per digit, making it O(d) where d is number of digits.
Space Complexity
Only a few variables are used, so space is constant O(1).
Which Approach is Fastest?
The division loop is efficient and uses constant space; string conversion is simpler but may use more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Division loop | O(d) | O(1) | Efficient for all integer sizes |
| String conversion | O(d) | O(d) | Simple code, good for small to medium numbers |
| Recursive division | O(d) | O(d) | Educational, but risky for large numbers |
Use
Math.Abs to handle negative numbers before counting digits.Forgetting to handle zero or negative numbers correctly can give wrong digit counts.