Posted on by Kalkicode
Code Number

Find the frequency of digits in a number

The problem at hand is to find the frequency of a given digit in a given integer number. In this context, the frequency of a digit refers to the number of times a particular digit appears in the given number. To solve this problem, we will use a C program that takes an integer number and a digit as input and calculates the frequency of the digit in the number.

Example

Let's consider the number 575657554 and want to find the frequency of the digit 5 in this number. The digit 5 appears five times in the number 575657554, so the output of the program should be: "Number : 575657554 digit : [5] frequency : 5".

Pseudocode

  1. Start the digit_frequency function, taking two integer inputs: number and digit.
  2. Check if the given digit is valid (between 0 and 9), and if not, return as it is an invalid input.
  3. If the number is negative, take its absolute value to work with positive numbers only.
  4. Initialize a variable "result" to 0 to store the frequency of the digit.
  5. Start a loop to iterate through each digit of the number: a. Get the last digit of the number using "number % 10". b. If the last digit is equal to the given digit, increment the result variable. c. Reduce the number by removing the last digit using "number /= 10". d. Repeat the loop until the number becomes 0.
  6. Print the output with the original number, the digit we are looking for, and its frequency.
  7. End the function.

Algorithm Explanation

The digit_frequency function takes two parameters: number and digit. The digit parameter is first checked to see if it is a valid digit (between 0 and 9). If the number is negative, it is converted to a positive number to ensure accurate calculations. The variable "result" is initialized to 0, which will be used to store the frequency of the digit in the number.

The algorithm then enters a while loop that continues until the number becomes 0. In each iteration, the last digit of the number is extracted using "number % 10". If this last digit is equal to the digit we are looking for, the "result" variable is incremented. Then, the last digit is removed from the number using "number /= 10", and the loop continues.

Once the loop finishes, the function prints the output, displaying the original number, the digit we are interested in, and its frequency in the number.

Code Solution

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

Resultant Output Explanation

Let's analyze the provided test cases and their respective outputs:

  1. digit_frequency(5121112, 1); Number : 5121112 digit : [1] frequency : 4 The digit 1 appears four times in the number 5121112.

  2. digit_frequency(575657554, 5); Number : 575657554 digit : [5] frequency : 5 The digit 5 appears five times in the number 575657554.

  3. digit_frequency(-4211221, 2); Number : -4211221 digit : [2] frequency : 3 The digit 2 appears three times in the absolute value of the number 4211221.

  4. digit_frequency(5211221, 7); Number : 5211221 digit : [7] frequency : 0 The digit 7 does not appear in the number 5211221.

Time Complexity

The time complexity of this code is O(log N), where N is the absolute value of the input number. The while loop iterates through each digit of the number, and the number of digits in N is proportional to log(N) (base 10). Therefore, the algorithm's time complexity is logarithmic, making it an efficient approach for finding the frequency of digits in a given number.

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.

New Comment