Contar el número de ocurrencias de un dígito particular en un número

Dado un número N y un dígito D , la tarea es contar las ocurrencias de D en N.
Ejemplos: 
 

Input: N = 25, D = 2
Output: 1

Input: N = 100, D = 0
Output: 2

Enfoque: Saque los dígitos uno por uno en N y verifique si este dígito es igual a D. Si es igual, incremente el conteo en 1. Al final, imprima el conteo.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to count the number of occurrences
// of a particular digit in a number
 
#include <iostream>
using namespace std;
 
// Function to count the occurrences
// of the digit D in N
long long int countoccurrences(long long int n,
                               int d)
{
    long long 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
int main()
{
 
    int d = 2;
    long long int n = 214215421;
 
    cout << countOccurrances(n, d)
         << endl;
 
    return 0;
}

Java

// Java program to count the number of occurrences
// of a particular digit in a number
import java.util.*;
 
class GFG
{
 
// Function to count the occurrences
// of the digit D in N
static int countoccurrences(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(String args[])
{
    int d = 2;
    int n = 214215421;
 
    System.out.println(countOccurrances(n, d));
}
}
 
// This code is contributed by Surendra_Gangwar

Python3

# Python3 program to count the
# number of occurrences of a
# particular digit in a number
 
# Function to count the occurrences
# of the digit D in N
def countoccurrences(n, d):
    count = 0
 
    # Loop to find the digits of N
    while (n > 0):
 
        # check if the digit is D
        if(n % 10 == d):
            count = count + 1
  
        n = n // 10
 
    # return the count of the
    # occurrences of D in N
    return count
 
# Driver code
d = 2
n = 214215421
print(countOccurrances(n, d))
 
# This code is contributed by Mohit Kumar

C#

// C# program to count the number
// of occurrences of a particular
// digit in a number
using System;
class GFG
{
 
// Function to count the occurrences
// of the digit D in N
static int countoccurrences(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(countOccurrances(n, d));
}
}
 
// This code is contributed by Code_Mech.

Javascript

<script>
 
// Javascript program to count
// the number of occurrences
// of a particular digit in a number
 
// Function to count the occurrences
// of the digit D in N
function countoccurrences(n, d)
{
    let 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 = parseInt(n / 10);
    }
 
    // return the count of the
    // occurrences of D in N
    return count;
}
 
// Driver code
 
    let d = 2;
    let n = 214215421;
 
    document.write(countOccurrances(n, d));
 
</script>
Producción: 

3

 

Publicación traducida automáticamente

Artículo escrito por Code_r y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *