C# Program to Count the Digits in an Integer

count digits

C# programs to count the digits in an integer have been shown here. For example, if a number is 12345, the total number of digits is 5. The algorithm, pseudocode and time complexity of the programs have also been covered below.






1. Algorithm to count the digits in an integer


1. Take a number x as input.

2. Initialize a counter to 0 say c = 0.

3. Perform x = x / 10 and Increment c by 1 i.e. c = c + 1

4. If x = 0 stop the process and display c as output else go to step 3.




2. Pseudocode to count the digits in an integer


Input : A number $n$

Output : Total no of digits in $n$

1. Procedure countDigits($n$):

2. $c \leftarrow 0$

3. If $n == 0$:

4. $c \leftarrow 1$

5. Else:

6. Repeat until $n \neq 0$

7. $n \leftarrow n / 10$

8. $c \leftarrow c + 1$

9. Return $c$

10. End Procedure





3. Time complexity to count the digits in an integer


Time Complexity: O(log(n))

Where n is the input number.





4. Program & output to count the digits in an integer




4.1. C# Program & output to count the digits in an integer using iteration

Code has been copied
/*****************************************
            alphabetacoder.com
 C# program to count the number of digits 
 in an integer using iterative approach
******************************************/

using System;
namespace DigitCount {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int num, count = 0;

            // take input of the number
            Console.Write("Enter the integer = ");
            num = Convert.ToInt32(Console.ReadLine());

            //count the digits
            if (num == 0)
                count = 1;
            else {
                while (num != 0) {
                    num = num / 10;
                    count++;
                }
            }

            // display result
            Console.WriteLine("Number of digits = " + count);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the integer = 66558899

Number of digits = 8



  1. The code begins with the declaration of the namespace DigitCount and the Program class.
  2. Inside the Program class, there is the Main method which serves as the entry point of the program.
  3. In the Main method, the code declares two variables: num to store the input integer and count to store the count of digits.
  4. The program prompts the user to enter an integer by displaying the message "Enter the integer = ".
  5. The user's input is read using Console.ReadLine() and converted to an integer using Convert.ToInt32() before being assigned to the num variable.
  6. The program checks if the input number num is equal to 0. If it is, it means that the number has only one digit (which is 0), and the count variable is set to 1.
  7. If the input number is not 0, the program enters a while loop. This loop continues as long as num is not equal to 0.
  8. Inside the while loop, the program divides num by 10 using the expression num = num / 10. This effectively removes the last digit from the number.
  9. The count variable is incremented by 1 in each iteration of the loop.
  10. Once the loop condition becomes false (i.e., num becomes 0), the program exits the loop.
  11. The program displays the result by printing "Number of digits = " concatenated with the value of count using Console.WriteLine().
  12. Finally, the program waits for the user to press any key before exiting, using Console.ReadKey().
  13. This program counts the number of digits in the input integer using an iterative approach. It uses a while loop to continuously divide the number by 10 until it becomes 0, incrementing the count of digits in each iteration.



4.2. C# Program & output to count the digits in an integer using recursion

Code has been copied
/*****************************************
            alphabetacoder.com
 C# program to count the number of digits 
 in an integer using recursive approach
******************************************/

using System;
namespace DigitCount {
    class Program {
        // recursive function to calculate
        // no of digits in an integer
        static int count_digit(int num) {
            if (num / 10 == 0)
                return 1;
            else
                // call function 
                return 1 + count_digit(num / 10);
        }

        static void Main(string[] args) {
            // declare variables
            int num;

            // take input of the number
            Console.Write("Enter the integer = ");
            num = Convert.ToInt32(Console.ReadLine());

            // display result by calling function
            Console.WriteLine("Number of digits = " + count_digit(num));

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the integer = 56478

Number of digits = 5



  1. The code begins with the declaration of the namespace DigitCount and the Program class.
  2. Inside the Program class, there is a static method called count_digit() which takes an integer num as a parameter and returns an integer.
  3. The count_digit() method is a recursive function that calculates the number of digits in an integer.
  4. In the count_digit() method, the code checks if num divided by 10 is equal to 0. This condition serves as the base case for the recursion. If the condition is true, it means that num has only one digit, and the method returns 1.
  5. If the base case condition is not met, the code calls the count_digit() method recursively with num divided by 10 as the argument. This recursive call continues until the base case is reached.
  6. Each recursive call adds 1 to the result returned by the subsequent recursive call. This is done by returning 1 + count_digit(num / 10).
  7. In the Main method, the code declares a variable num to store the input integer.
  8. The program prompts the user to enter an integer by displaying the message "Enter the integer = ".
  9. The user's input is read using Console.ReadLine() and converted to an integer using Convert.ToInt32() before being assigned to the num variable.
  10. The program then calls the count_digit method with num as the argument to calculate the number of digits in the input integer.
  11. The result of the count_digit() method is then displayed by printing "Number of digits = " concatenated with the value returned by the count_digit() method, using Console.WriteLine().
  12. Finally, the program waits for the user to press any key before exiting, using Console.ReadKey().
  13. This program counts the number of digits in the input integer using a recursive approach. The recursive function count_digit() repeatedly divides the number by 10 until it reaches the base case of having only one digit, and then returns the count of digits.



4.3. C# Program & output to count the digits in an integer using logarithm

Code has been copied
/**************************************
           alphabetacoder.com
 C# program to count the number of 
 digits in an integer using logarithm
***************************************/

using System;
namespace DigitCount {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int num, d;

            // take input of the number
            Console.Write("Enter the integer = ");
            num = Convert.ToInt32(Console.ReadLine());

            if (num == 0) // if input number = 0
                Console.WriteLine("Number of digits = 1");
            else {
                // calculate result using log10
                d = (int) Math.Floor(Math.Log10(num) + 1);
                // display result
                Console.WriteLine("Number of digits = " + d);
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the integer = 3456

Number of digits = 4



  1. The code begins with the declaration of the namespace DigitCount and the Program class.
  2. Inside the Main method, the code declares two variables: num to store the input integer and d to store the count of digits.
  3. The program prompts the user to enter an integer by displaying the message "Enter the integer = ".
  4. The user's input is read using Console.ReadLine() and converted to an integer using Convert.ToInt32() before being assigned to the num variable.
  5. The code then checks if the input number is equal to 0 using the if statement. If it is, it means that the number has only one digit (which is 0), and the program displays "Number of digits = 1" using Console.WriteLine().
  6. If the input number is not 0, the code proceeds to calculate the number of digits using the logarithm base 10.
  7. The line d = (int)Math.Floor(Math.Log10(num) + 1); performs the calculation. It calculates the logarithm base 10 of the input number num using Math.Log10(num), then adds 1 to the result using + 1. The Math.Floor method is used to round down the result to the nearest integer. Finally, the result is cast to an integer using (int) and assigned to the d variable.
  8. After calculating the number of digits, the code displays the result by printing "Number of digits = " concatenated with the value of d using Console.WriteLine().
  9. Finally, the program waits for the user to press any key before exiting, using Console.ReadKey().
This program effectively counts the number of digits in the input integer by using the logarithm base 10.