C# Program - Count digits in an Integer
In C#, count of digits in a given integer can be found out by using below mentioned methods.
Method 1: Using iteration
The method involves the following steps:
Input: MyNum Step 1: Initialize the count = 0 Step 2: Iterate over MyNum while it is not zero. Step 2a: Update MyNum by MuNum / 10 Step 2b: Increase count by 1 Step 3: Return count
Example:
Input: 564 count: 0 Iteration 1: MyNum: 564 / 10 = 56 count: 0 + 1 = 1 Iteration 2: MyNum: 56 / 10 = 5 count: 1 + 1 = 2 Iteration 3: MyNum: 5 / 10 = 0 count: 2 + 1 = 3 return count = 3
The below block of code shows the implementation of above concept:
using System; class MyProgram { //method to count number of digits static int countDigits(long MyNum) { int count = 0; while(MyNum != 0){ MyNum = MyNum / 10; count++; } return count; } static void Main(string[] args) { long x = 123; long y = 459709; Console.WriteLine("{0} contains: {1} digits", x, countDigits(x)); Console.WriteLine("{0} contains: {1} digits", y, countDigits(y)); } }
The above code will give the following output:
123 contains: 3 digits 459709 contains: 6 digits
Method 2: Using Recursion
The above result can also be achieved using recursive method. Consider the example below:
using System; class MyProgram { //method to count number of digits static int countDigits(long MyNum) { if(MyNum != 0) return 1 + countDigits(MyNum/10); else return 0; } static void Main(string[] args) { long x = 564; long y = 980620; Console.WriteLine("{0} contains: {1} digits", x, countDigits(x)); Console.WriteLine("{0} contains: {1} digits", y, countDigits(y)); } }
The above code will give the following output:
564 contains: 3 digits 980620 contains: 6 digits
Method 3: Using log method
The log (base-10) of absolute value of a given number can be used to find out the number of digits in a given number. Consider the example below:
using System; class MyProgram { //method to count number of digits static int countDigits(long MyNum) { return (int)Math.Log10(Math.Abs(MyNum)) + 1; } static void Main(string[] args) { long x = 564; long y = -12345; long z = 980620; Console.WriteLine("{0} contains: {1} digits", x, countDigits(x)); Console.WriteLine("{0} contains: {1} digits", y, countDigits(y)); Console.WriteLine("{0} contains: {1} digits", z, countDigits(z)); } }
The above code will give the following output:
564 contains: 3 digits -12345 contains: 5 digits 980620 contains: 6 digits
Method 4: Using length of string
This can also be achieved by converting the absolute value of number into a string and then finding out the length of the string to get the number of digits in the original number. Consider the example below:
using System; class MyProgram { //method to count number of digits static int countDigits(long MyNum) { string MyStr = Convert.ToString(MyNum); return MyStr.Length; } static void Main(string[] args) { long x = 564; long y = -12345; long z = 980620; Console.WriteLine("{0} contains: {1} digits", x, countDigits(x)); Console.WriteLine("{0} contains: {1} digits", y, countDigits(y)); Console.WriteLine("{0} contains: {1} digits", z, countDigits(z)); } }
The above code will give the following output:
564 contains: 3 digits -12345 contains: 5 digits 980620 contains: 6 digits
Recommended Pages
- C# - Swap two numbers
- C# Program - Fibonacci Sequence
- C# Program - Insertion Sort
- C# Program - Find Factorial of a Number
- C# Program - Find HCF of Two Numbers
- C# Program - Merge Sort
- C# Program - Shell Sort
- Stack in C#
- Queue in C#
- C# Program - Find LCM of Two Numbers
- C# Program - To Check Whether a Number is Palindrome or Not
- C# Program - To Check Whether a String is Palindrome or Not
- C# Program - Heap Sort
- C# Program - Quick Sort
- C# - Swap Two Numbers without using Temporary Variable
- C# Program - To Check Armstrong Number
- C# Program - Counting Sort
- C# Program - Radix Sort
- C# Program - Find Largest Number among Three Numbers
- C# Program - Print Floyd's Triangle