# Find the frequency of each digit in a number

Here given code implementation process.

``````//C Program
//Find the frequency of each digit in a number
#include <stdio.h>

//Find the occurrence of digits in number
void occurrence(int number, int result[])
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
void frequency(int number)
{
//This is used to store result of number digit frequency
int result[10];
//Set initial frequency of digit
for (int i = 0; i < 10; ++i)
{
result[i] = 0;
}
printf("\nNumber :  %d", number);
occurrence(number, result);
for (int i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
printf("\n %d  : %d", i, result[i]);
}
}
printf("\n");
}
int main()
{
//Test Cases
frequency(1234);
frequency(7831432);
frequency(5121112);
frequency(-4211221);
frequency(1177714);
return 0;
}``````

#### Output

``````Number :  1234
1  : 1
2  : 1
3  : 1
4  : 1

Number :  7831432
1  : 1
2  : 1
3  : 2
4  : 1
7  : 1
8  : 1

Number :  5121112
1  : 4
2  : 2
5  : 1

Number :  -4211221
1  : 3
2  : 3
4  : 1

Number :  1177714
1  : 3
4  : 1
7  : 3``````
``````/*
C++ Program
Find the frequency of each digit in a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
public:
//Find the occurrence of digits in number
void occurrence(int number, int result[])
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
void frequency(int number)
{
int *result = new int[10];
//Set initial frequency of digit
for (int i = 0; i < 10; ++i)
{
result[i] = 0;
}
cout << "\nNumber : " << number << "";
this->occurrence(number, result);
for (int i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
cout << "\n " << i << " : " << result[i] << "";
}
}
cout << "\n";
}
};
int main()
{
MyNumber obj =  MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
return 0;
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
Java Program
Find the frequency of each digit in a number
*/
public class MyNumber
{
//Find the occurrence of digits in number
public void occurrence(int number, int[] result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
public void frequency(int number)
{
//This is used to store result of number digit frequency
int[] result = new int[10];
//Set initial frequency of digit
for (int i = 0; i < 10; ++i)
{
result[i] = 0;
}
System.out.print("\nNumber : " + number + "");
occurrence(number, result);
for (int i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
System.out.print("\n " + i + " : " + result[i] + "");
}
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
C# Program
Find the frequency of each digit in a number
*/
using System;
public class MyNumber
{
//Find the occurrence of digits in number
public void occurrence(int number, int[] result)
{
int temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number /= 10;
}
}
public void frequency(int number)
{
int[] result = new int[10];
//Set initial frequency of digit
for (int i = 0; i < 10; i++)
{
result[i] = 0;
}
Console.Write("\nNumber : " + number + "");
occurrence(number, result);
for (int i = 0; i < 10; i++)
{
if (result[i] != 0)
{
Console.Write("\n " + i + " : " + result[i] + "");
}
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````<?php
/*
Php Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
public 	function occurrence(\$number, & \$result)
{
\$temp = 0;
while (\$number != 0)
{
\$temp = \$number % 10;
if (\$temp < 0)
{
\$temp = -\$temp;
}
\$result[\$temp]++;
\$number = intval(\$number / 10);
}
}
public 	function frequency(\$number)
{
//This is used to store result of number digit frequency
\$result = array_fill(0, 10, 0);
echo("\nNumber : ". \$number ."");
\$this->occurrence(\$number, \$result);
for (\$i = 0; \$i < 10; ++\$i)
{
if (\$result[\$i] != 0)
{
echo("\n ". \$i ." : ". \$result[\$i] ."");
}
}
echo("\n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Case
\$obj->frequency(1234);
\$obj->frequency(7831432);
\$obj->frequency(5121112);
\$obj->frequency(-4211221);
\$obj->frequency(1177714);
}
main();``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
Node Js Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
occurrence(number, result)
{
var temp = 0;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp]++;
number = parseInt(number / 10);
}
}
frequency(number)
{
//This is used to store result of number digit frequency
var result = Array(10).fill(0);
process.stdout.write("\nNumber : " + number + "");
this.occurrence(number, result);
for (var i = 0; i < 10; ++i)
{
if (result[i] != 0)
{
process.stdout.write("\n " + i + " : " + result[i] + "");
}
}
process.stdout.write("\n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
main();``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````#   Python 3 Program
#   Find the frequency of each digit in a number

class MyNumber :
# Find the occurrence of digits in number
def occurrence(self, number, result) :
temp = 0
while (number != 0) :
temp = number % 10
if (temp < 0) :
temp = -temp

result[temp] += 1
number = int(number / 10)

def frequency(self, number) :
# Set initial frequency of digit
result = [0] * 10
i = 0
print("\nNumber : ", number ,"", end = "")
if (number < 0) :
number = -number

self.occurrence(number, result)
i = 0
while (i < 10) :
if (result[i] != 0) :
print("\n ", i ," : ", result[i] ,"", end = "")

i += 1

print("\n", end = "")

def main() :
obj = MyNumber()
# Test Case
obj.frequency(1234)
obj.frequency(7831432)
obj.frequency(5121112)
obj.frequency(-4211221)
obj.frequency(1177714)

if __name__ == "__main__": main()``````

#### Output

``````Number :  1234
1  :  1
2  :  1
3  :  1
4  :  1

Number :  7831432
1  :  1
2  :  1
3  :  2
4  :  1
7  :  1
8  :  1

Number :  5121112
1  :  4
2  :  2
5  :  1

Number :  -4211221
1  :  3
2  :  3
4  :  1

Number :  1177714
1  :  3
4  :  1
7  :  3``````
``````#   Ruby Program
#   Find the frequency of each digit in a number

class MyNumber

# Find the occurrence of digits in number
def occurrence(number, result)

temp = 0
while (number != 0)

temp = number % 10
if (temp < 0)

temp = -temp
end
result[temp] += 1
number /= 10
end
end
def frequency(number)

# Set initial frequency of digit
result = Array.new(10) {0}
i = 0
print("\nNumber  : ", number ,"")
if (number < 0)

number = -number
end
self.occurrence(number, result)
i = 0
while (i < 10)

if (result[i] != 0)

print("\n ", i ,"  : ", result[i] ,"")
end
i += 1
end
print("\n")
end
end
def main()

obj = MyNumber.new()
# Test Case
obj.frequency(1234)
obj.frequency(7831432)
obj.frequency(5121112)
obj.frequency(-4211221)
obj.frequency(1177714)
end
main()``````

#### Output

``````Number  : 1234
1  : 1
2  : 1
3  : 1
4  : 1

Number  : 7831432
1  : 1
2  : 1
3  : 2
4  : 1
7  : 1
8  : 1

Number  : 5121112
1  : 4
2  : 2
5  : 1

Number  : -4211221
1  : 3
2  : 3
4  : 1

Number  : 1177714
1  : 3
4  : 1
7  : 3
``````
``````/*
Scala Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
def occurrence(num: Int, result: Array[Int]): Unit = {
var temp: Int = 0;
var number: Int = num;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result(temp) += 1;
number = (number / 10).toInt;
}
}
def frequency(num: Int): Unit = {
//Set initial frequency of digit
var result: Array[Int] = Array.fill[Int](10)(0);
var i: Int = 0;
var number: Int = num;
print("\nNumber : " + number + "");
if (number < 0)
{
number = -number;
}
occurrence(number, result);
i = 0;
while (i < 10)
{
if (result(i) != 0)
{
print("\n " + i + " : " + result(i) + "");
}
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
}``````

#### Output

``````Number : 1234
1 : 1
2 : 1
3 : 1
4 : 1

Number : 7831432
1 : 1
2 : 1
3 : 2
4 : 1
7 : 1
8 : 1

Number : 5121112
1 : 4
2 : 2
5 : 1

Number : -4211221
1 : 3
2 : 3
4 : 1

Number : 1177714
1 : 3
4 : 1
7 : 3``````
``````/*
Swift Program
Find the frequency of each digit in a number
*/
class MyNumber
{
//Find the occurrence of digits in number
func occurrence(_ num:  Int, _ result: inout [Int])
{
var temp: Int = 0;
var number: Int = num;
while (number != 0)
{
temp = number % 10;
if (temp < 0)
{
temp = -temp;
}
result[temp] += 1;
number /= 10;
}
}
func frequency(_ num: Int)
{
//Set initial frequency of digit
var result: [Int] = Array(repeating: 0, count: 10);
var i: Int = 0;
var number: Int = num;
print("\nNumber : ", number ,"", terminator: "");
if (number < 0)
{
number = -number;
}
self.occurrence(number, &result);
i = 0;
while (i < 10)
{
if (result[i] != 0)
{
print("\n ", i ," : ", result[i] ,"", terminator: "");
}
i += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Case
obj.frequency(1234);
obj.frequency(7831432);
obj.frequency(5121112);
obj.frequency(-4211221);
obj.frequency(1177714);
}
main();``````

#### Output

``````Number :  1234
1  :  1
2  :  1
3  :  1
4  :  1

Number :  7831432
1  :  1
2  :  1
3  :  2
4  :  1
7  :  1
8  :  1

Number :  5121112
1  :  4
2  :  2
5  :  1

Number :  -4211221
1  :  3
2  :  3
4  :  1

Number :  1177714
1  :  3
4  :  1
7  :  3``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.