program DigitFrequency;
{
Function: countDigitFrequency
Purpose: Counts how many times each digit (0–9) appears in a number.
Parameters:
- n: the number whose digits we want to count
- freq: an array of size 10 that stores the frequency of each digit
freq[0] = count of digit '0'
freq[1] = count of digit '1'
...
freq[9] = count of digit '9'
Explanation:
We repeatedly extract the last digit using n mod 10,
then remove that digit using n div 10.
}
procedure countDigitFrequency(n: LongInt; var freq: array of Integer);
var
digit: Integer;
begin
{ Process each digit of the number }
while n > 0 do
begin
digit := n mod 10; { extract last digit }
freq[digit] := freq[digit] + 1; { increase its frequency }
n := n div 10; { remove last digit }
end;
end;
var
n: LongInt; { the number we want to analyze }
freq: array[0..9] of Integer; { array to store digit frequencies }
i: Integer;
begin
n := 79712622;
{ Initialize frequencies }
for i := 0 to 9 do
freq[i] := 0;
{ Call the function to count digit frequencies }
countDigitFrequency(n, freq);
{ Display the result }
writeln('Digit frequencies in ', n, ':');
writeln;
for i := 0 to 9 do
if freq[i] <> 0 then
writeln('Digit ', i, ' occurs ', freq[i], ' times');
end.
{
run:
Digit frequencies in 79712622:
Digit 1 occurs 1 times
Digit 2 occurs 3 times
Digit 6 occurs 1 times
Digit 7 occurs 2 times
Digit 9 occurs 1 times
}