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.
Page content(s):
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.
3.
4.
5.
6.
7.
8.
9.
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
/***************************************** 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
- The code begins with the declaration of the namespace DigitCount and the Program class.
- Inside the Program class, there is the Main method which serves as the entry point of the program.
- In the Main method, the code declares two variables: num to store the input integer and count to store the count of digits.
- The program prompts the user to enter an integer by displaying the message "Enter the integer = ".
- The user's input is read using Console.ReadLine() and converted to an integer using Convert.ToInt32() before being assigned to the num variable.
- 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.
- 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.
- 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.
- The count variable is incremented by 1 in each iteration of the loop.
- Once the loop condition becomes false (i.e., num becomes 0), the program exits the loop.
- The program displays the result by printing "Number of digits = " concatenated with the value of count using Console.WriteLine().
- Finally, the program waits for the user to press any key before exiting, using Console.ReadKey(). 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
/***************************************** 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
- The code begins with the declaration of the namespace DigitCount and the Program class.
- Inside the Program class, there is a static method called count_digit() which takes an integer num as a parameter and returns an integer.
- The count_digit() method is a recursive function that calculates the number of digits in an integer.
- 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.
- 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.
- Each recursive call adds 1 to the result returned by the subsequent recursive call. This is done by returning 1 + count_digit(num / 10).
- In the Main method, the code declares a variable num to store the input integer.
- The program prompts the user to enter an integer by displaying the message "Enter the integer = ".
- The user's input is read using Console.ReadLine() and converted to an integer using Convert.ToInt32() before being assigned to the num variable.
- The program then calls the count_digit method with num as the argument to calculate the number of digits in the input integer.
- 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().
- Finally, the program waits for the user to press any key before exiting, using Console.ReadKey(). 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
/************************************** 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
- The code begins with the declaration of the namespace DigitCount and the Program class.
- Inside the Main method, the code declares two variables: num to store the input integer and d to store the count of digits.
- The program prompts the user to enter an integer by displaying the message "Enter the integer = ".
- The user's input is read using Console.ReadLine() and converted to an integer using Convert.ToInt32() before being assigned to the num variable.
- 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().
- If the input number is not 0, the code proceeds to calculate the number of digits using the logarithm base 10.
- 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.
- 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().
- Finally, the program waits for the user to press any key before exiting, using Console.ReadKey().