找出数字中某个数字的频率

给定一个数字N和一个数字D。编写一个程序,找出数字N中出现数字D的次数。 例如:

null
Input: N = 1122322  ,  D = 2Output: 4Input: N = 346488  ,  D = 9Output: 0

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

C++

// C++ program to find the frequency
// of a digit in a number
#include <bits/stdc++.h>
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<<frequencyDigits(N,D);
return 0;
}


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

<?php
// PHP program to find the frequency
// of a digit in a number
// function to find frequency
// of digit  in a number
function 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 ++;
// 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

<script>
// Javascript program to find
// the frequency of a
// digit in a number
// Function to find frequency
// of digit in a number
function frequencyDigits(n, d)
{
// Counter variable to
// store the frequency
var c = 0;
// Iterate till number
// reduces to zero
while (n > 0)
{
// Check for equality
if (n % 10 == d)
c++;
// Reduce the number
n = parseInt(n / 10);
}
return c;
}
// Driver code
// input number N
var N = 1122322;
// input digit D
var D = 2;
document.write(frequencyDigits(N, D));
// This code is contributed by Khushboogoyal499
</script>


输出:

4

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享