> C# program to find the occurrence of a digit in a number
> C# program to find frequency of digit in a given number

In the previous posts, i have given C#.Net code to count the occurrences of a character in a string

This program is to count the occurrences of a digit in a given number.

I have written 3 different methods for the same thing.

The first method is quite simple and it doesn’t use any Array, List, built-in methods or Linq expressions.
using System;
using System.Collections.Generic;
using System.Linq;
namespaceDigitCountInNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, count = 0;
            byte digitN; //size:1 byte, Range:0 to 255
            Console.WriteLine(“\n *** www.programmingposts.com ***\n”);
            Console.WriteLine(” >>>> To count ‘n’ digit in a Number <<<< “);
            try
            {
                Console.Write(“\n Enter a integer number greater than zero: “);
                num = Convert.ToInt32(Console.ReadLine());
                if (num <= 0)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine(“\n Expected number greater than zero.”);
                    Console.ReadLine(); return;
                }
                Console.Write(“\n Enter single digit to be counted in number: “);
                digitN = Convert.ToByte(Console.ReadLine());
                if (digitN > 0 && digitN <= 9)
                {
                    count = countNDigitInNumber(num, digitN);

                    //count = countNDigitInNumberUsingArray(num, digitN);

                    //count = countNDigitInNumberUsingList(num, digitN);
                    Console.WriteLine(“\n The count of {0} in {1} is : {2} “, digitN, num, count);
                }
                else
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine(“\n single digit number expected between 0,9.”);
                }
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
        static int countNDigitInNumber(int num, byte digitN)
        {
            int rem, temp, count = 0;
            temp = num;
            while (num > 0)
            {
                rem = num % 10;  //for getting remainder by modulus with 10
                if (digitN == rem)
                {
                    count++;
                }
                num = num / 10; //for getting quotient by dividing with 10
            }
            return count;
        }
        static int countNDigitInNumberUsingArray(int num, byte digitN)
        {
            int count = 0;
            //converting number to single digit int array
            int[] digits = num.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();
            //getting count of digitN
            count = digits.Where(i => i == digitN).Count();
            return count;
        }
        static int countNDigitInNumberUsingList(int num, byte digitN)
        {
            int count = 0;
            //converting number to list of int
            List<int> lstdigits = num.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToList<int>();
            //getting count of digitN
            count = lstdigits.Where(i => i == digitN).Count();
            return count;
        }
    }
}
 

Sample outputs:

Leave a Reply

Your email address will not be published. Required fields are marked *