How to check if an integer include specific digits x times in Pascal

1 Answer

0 votes
program CheckDigitOccurrences;

function CheckDigitOccurrences(n: int64; xtims: Integer; digit: Integer): Boolean;
var
  count: Integer;
begin
  count := 0;

  // Count occurrences of the digit in the number
  while n > 0 do
  begin
    if (n mod 10 = digit) then
      Inc(count);
    n := n div 10;
  end;

  // Return true if the count matches xtims
  CheckDigitOccurrences := (count = xtims);
end;

begin
  // Test the function with the given inputs
  WriteLn(CheckDigitOccurrences(7097175, 3, 7)); 
  WriteLn(CheckDigitOccurrences(70975, 3, 7));   
end.



(*
run:

TRUE
FALSE

*)

 



answered Apr 26, 2025 by avibootz
...