Find the frequency of digits in a number

Here given code implementation process.

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

void digit_frequency(int number, int digit)
{
	if (digit > 9 || digit < 0)
	{
		//invalid digit
		return;
	}
	printf("Number : %d digit : [%d]", number, digit);
	if (number < 0)
	{
		number = -number;
	}
	int result = 0;
	while (number != 0)
	{
		if (number % 10 == digit)
		{
			//when digit are occured
			result++;
		}
		number /= 10;
	}
	printf(" frequency : %d\n", result);
}
int main()
{
	//Test Case
	digit_frequency(5121112, 1);
	digit_frequency(575657554, 5);
	digit_frequency(-4211221, 2);
	digit_frequency(5211221, 7);
	return 0;
}

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
/*
  C++ Program
  Find the frequency of digits in a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public: void digit_frequency(int number, int digit)
	{
		if (digit > 9 || digit < 0)
		{
			//invalid digit
			return;
		}
		cout << "Number : " << number << " digit : [" << digit << "]";
		if (number < 0)
		{
			number = -number;
		}
		int result = 0;
		while (number != 0)
		{
			if (number % 10 == digit)
			{
				//when digit are occured
				result++;
			}
			number /= 10;
		}
		cout << " frequency : " << result << "\n";
	}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Case 
	obj.digit_frequency(5121112, 1);
	obj.digit_frequency(575657554, 5);
	obj.digit_frequency(-4211221, 2);
	obj.digit_frequency(5211221, 7);
	return 0;
}

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
/*
  Java Program
  Find the frequency of digits in a number
*/
public class MyNumber
{
	public void digit_frequency(int number, int digit)
	{
		if (digit > 9 || digit < 0)
		{
			//invalid digit
			return;
		}
		System.out.print("Number : " + number + " digit : [" + digit + "]");
		if (number < 0)
		{
			number = -number;
		}
		int result = 0;
		while (number != 0)
		{
			if (number % 10 == digit)
			{
				//when digit are occured
				result++;
			}
			number /= 10;
		}
		System.out.print(" frequency : " + result + "\n");
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case 
		obj.digit_frequency(5121112, 1);
		obj.digit_frequency(575657554, 5);
		obj.digit_frequency(-4211221, 2);
		obj.digit_frequency(5211221, 7);
	}
}

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
/*
  C# Program
  Find the frequency of digits in a number
*/
using System;
public class MyNumber
{
	public void digit_frequency(int number, int digit)
	{
		if (digit > 9 || digit < 0)
		{
			//invalid digit
			return;
		}
		Console.Write("Number : " + number + " digit : [" + digit + "]");
		if (number < 0)
		{
			number = -number;
		}
		int result = 0;
		while (number != 0)
		{
			if (number % 10 == digit)
			{
				//when digit are occured
				result++;
			}
			number /= 10;
		}
		Console.Write(" frequency : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case 
		obj.digit_frequency(5121112, 1);
		obj.digit_frequency(575657554, 5);
		obj.digit_frequency(-4211221, 2);
		obj.digit_frequency(5211221, 7);
	}
}

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
<?php
/*
  Php Program
  Find the frequency of digits in a number
*/
class MyNumber
{
	public 	function digit_frequency($number, $digit)
	{
		if ($digit > 9 || $digit < 0)
		{
			return;
		}
		echo("Number : ". $number ." digit : [". $digit ."]");
		if ($number < 0)
		{
			$number = -$number;
		}
		$result = 0;
		while ($number != 0)
		{
			if ($number % 10 == $digit)
			{
				//when digit are occured
				$result++;
			}
			$number = intval($number / 10);
		}
		echo(" frequency : ". $result ."\n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Case 
	$obj->digit_frequency(5121112, 1);
	$obj->digit_frequency(575657554, 5);
	$obj->digit_frequency(-4211221, 2);
	$obj->digit_frequency(5211221, 7);
}
main();

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
/*
  Node Js Program
  Find the frequency of digits in a number
*/
class MyNumber
{
	digit_frequency(number, digit)
	{
		if (digit > 9 || digit < 0)
		{
			return;
		}
		process.stdout.write("Number : " + number + " digit : [" + digit + "]");
		if (number < 0)
		{
			number = -number;
		}
		var result = 0;
		while (number != 0)
		{
			if (number % 10 == digit)
			{
				//when digit are occured
				result++;
			}
			number = parseInt(number / 10);
		}
		process.stdout.write(" frequency : " + result + "\n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Case 
	obj.digit_frequency(5121112, 1);
	obj.digit_frequency(575657554, 5);
	obj.digit_frequency(-4211221, 2);
	obj.digit_frequency(5211221, 7);
}
main();

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
#   Python 3 Program
#   Find the frequency of digits in a number

class MyNumber :
	def digit_frequency(self, number, digit) :
		if (digit > 9 or digit < 0) :
			return
		
		print("Number : ", number ," digit : [", digit ,"]", end = "")
		if (number < 0) :
			number = -number
		
		result = 0
		while (number != 0) :
			if (number % 10 == digit) :
				# when digit are occured
				result += 1
			
			number = int(number / 10)
		
		print(" frequency : ", result ,"\n", end = "")
	

def main() :
	obj = MyNumber()
	# Test Case 
	obj.digit_frequency(5121112, 1)
	obj.digit_frequency(575657554, 5)
	obj.digit_frequency(-4211221, 2)
	obj.digit_frequency(5211221, 7)


if __name__ == "__main__": main()

Output

Number :  5121112  digit : [ 1 ] frequency :  4
Number :  575657554  digit : [ 5 ] frequency :  5
Number :  -4211221  digit : [ 2 ] frequency :  3
Number :  5211221  digit : [ 7 ] frequency :  0
#   Ruby Program
#   Find the frequency of digits in a number

class MyNumber

	def digit_frequency(number, digit)
	
		if (digit > 9 || digit < 0)
		
			return
		end
		print("Number  : ", number ," digit  : [", digit ,"]")
		if (number < 0)
		
			number = -number
		end
		result = 0
		while (number != 0)
		
			if (number % 10 == digit)
			
				# when digit are occured
				result += 1
			end
			number /= 10
		end
		print(" frequency  : ", result ,"\n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Case 
	obj.digit_frequency(5121112, 1)
	obj.digit_frequency(575657554, 5)
	obj.digit_frequency(-4211221, 2)
	obj.digit_frequency(5211221, 7)
end
main()

Output

Number  : 5121112 digit  : [1] frequency  : 4
Number  : 575657554 digit  : [5] frequency  : 5
Number  : -4211221 digit  : [2] frequency  : 3
Number  : 5211221 digit  : [7] frequency  : 0
/*
  Scala Program
  Find the frequency of digits in a number
*/
class MyNumber
{
	def digit_frequency(num : Int, digit : Int): Unit = {
		if (digit > 9 || digit < 0)
		{
			return;
		} 
      	var number : Int = num;
		print("Number : " + number + " digit : [" + digit + "]");
		if (number < 0)
		{
			number = -number;
		}
		var result: Int = 0;
		while (number != 0)
		{
			if (number % 10 == digit)
			{
				//when digit are occured
				result += 1;
			}
			number = (number / 10).toInt;
		}
		print(" frequency : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case 
		obj.digit_frequency(5121112, 1);
		obj.digit_frequency(575657554, 5);
		obj.digit_frequency(-4211221, 2);
		obj.digit_frequency(5211221, 7);
	}
}

Output

Number : 5121112 digit : [1] frequency : 4
Number : 575657554 digit : [5] frequency : 5
Number : -4211221 digit : [2] frequency : 3
Number : 5211221 digit : [7] frequency : 0
/*
  Swift Program
  Find the frequency of digits in a number
*/
class MyNumber
{
	func digit_frequency(_ num:  Int, _ digit: Int)
	{
		if (digit > 9 || digit < 0)
		{
			return;
		}
      	var number : Int = num;
		print("Number : ", number ," digit : [", digit ,"]", terminator: "");
		if (number < 0)
		{
			number = -number;
		}
		var result: Int = 0;
		while (number != 0)
		{
			if (number % 10 == digit)
			{
				//when digit are occured
				result += 1;
			}
			number /= 10;
		}
		print(" frequency : ", result ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Case 
	obj.digit_frequency(5121112, 1);
	obj.digit_frequency(575657554, 5);
	obj.digit_frequency(-4211221, 2);
	obj.digit_frequency(5211221, 7);
}
main();

Output

Number :  5121112  digit : [ 1 ] frequency :  4
Number :  575657554  digit : [ 5 ] frequency :  5
Number :  -4211221  digit : [ 2 ] frequency :  3
Number :  5211221  digit : [ 7 ] frequency :  0


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.

New Comment







© 2021, kalkicode.com, All rights reserved