📜  找出数字中数字的频率

📅  最后修改于: 2021-05-31 22:10:47             🧑  作者: Mango

给定数字N和数字D。编写一个程序以查找数字D在数字N中出现了多少次。
例子 :

Input: N = 1122322  ,  D = 2
Output: 4

Input: N = 346488  ,  D = 9
Output: 0

解决此问题的想法是继续从数字N中提取数字,并用给定的数字D检查提取的数字。如果提取的数字等于数字D,则增加计数。
下面是上述方法的实现。

C++
// C++ program to find the frequency
// of a digit in a number
#include 
using namespace std;
 
// function to find frequency of digit
// in a number
int frequencyDigits(int n, int d)
{  
    // Counter variable to store
    // the frequency
    int c = 0;
     
    // iterate till number reduces to zero
    while (n > 0) {
         
        // check for equality
        if (n % 10 == d)
            c++;
        // reduce the number
        n = n / 10;
    }
     
    return c;
}
 
// Driver Code
int main()
{
     
    // input number N
    int N = 1122322;
     
    // input digit D
    int D = 2;
     
    cout<


Java
// Java program to find
// the frequency of a
// digit in a number
class GFG
{
 
// function to find frequency
// of digit in a number
static int frequencyDigits(int n,
                           int d)
{
    // Counter variable to
    // store the frequency
    int c = 0;
     
    // iterate till number
    // reduces to zero
    while (n > 0)
    {
         
        // check for equality
        if (n % 10 == d)
            c++;
        // reduce the number
        n = n / 10;
    }
    return c;
}
 
// Driver Code
public static void main(String args[])
{
     
    // input number N
    int N = 1122322;
     
    // input digit D
    int D = 2;
     
    System.out.println(frequencyDigits(N, D));
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 program to find the
# frequency of a digit in a number
 
# function to find frequency
# of digit in a number
def frequencyDigits(n, d):
     
    # Counter variable to
    # store the frequency
    c = 0;
     
    # iterate till number
    # reduces to zero
    while (n > 0):
         
        # check for equality
        if (n % 10 == d):
            c += 1;
        # reduce the number
        n = int(n / 10);
 
    return c;
 
# Driver Code
 
# input number N
N = 1122322;
 
# input digit D
D = 2;
 
print(frequencyDigits(N, D));
 
# This code is contributed by mits


C#
// C# program to find the frequency
// of a digit in a number
using System;
 
class GFG
{
 
// function to find frequency
// of digit in a number
static int frequencyDigits(int n,
                           int d)
{
    // Counter variable to
    // store the frequency
    int c = 0;
     
    // iterate till number
    // reduces to zero
    while (n > 0)
    {
         
        // check for equality
        if (n % 10 == d)
            c++;
        // reduce the number
        n = n / 10;
    }
    return c;
}
 
// Driver Code
static public void Main(String []args)
{
     
    // input number N
    int N = 1122322;
     
    // input digit D
    int D = 2;
     
    Console.WriteLine(frequencyDigits(N, D));
 
}
}
 
// This code is contributed by Arnab Kundu


PHP
 0)
    {
         
        // check for equality
        if ($n % 10 == $d)
            $c++;
        // reduce the number
        $n = $n / 10;
    }
     
    return $c;
}
 
// Driver Code
 
// input number N
$N = 1122322;
 
// input digit D
$D = 2;
 
echo frequencyDigits($N, $D);
 
// This code is contributed by mits
?>


Javascript


输出 :
4
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”