C# Program to count particular digit in a given number
- This program gives the result as count of a particular digit in the given number.
class CountPerticularNum
{
static int countOccurrances(int n,int d)
{
int count = 0;
// Loop to find the digits of N
while (n > 0)
{
// check if the digit is D
count = (n % 10 == d) ?
count + 1 : count;
n = n / 10;
}
// return the count of the
// occurrences of D in N
return count;
}
// Driver code
public static void Main()
{
int d = 2;
int n = 214215421;
Console.WriteLine("Number is : "+n);
Console.WriteLine("The count of "+ n +" digit in a Number is :"+ n);
Console.ReadLine();
}
}
No comments