How to check if a number is automorphic number in Pascal

1 Answer

0 votes
program AutomorphicNumber;

// An Automorphic number is a number whose square ends with the same digits 
// as the original number. E.g – 5 : 5 * 5 = 25 //ends with 5 

uses
  SysUtils; // IntToStr

function IntPower10(n: Integer): LongInt;
var
  i: Integer;
  resultVal: LongInt;
begin
  resultVal := 1;
  for i := 1 to n do
    resultVal := resultVal * 10;
  IntPower10 := resultVal;
end;

function CheckAutomorphicNumber(num: Integer): Boolean;
var
  s: String;
  square, last, power: LongInt;
begin
  s := IntToStr(num);
  square := num * num;
  power := IntPower10(Length(s));   { compute 10^Length(s) safely }
  last := square mod power;

  Writeln(num, ' square = ', square);

  CheckAutomorphicNumber := (last = num);
end;

begin
  Writeln(CheckAutomorphicNumber(25));  
  Writeln(CheckAutomorphicNumber(5));   
  Writeln(CheckAutomorphicNumber(76));  
  Writeln(CheckAutomorphicNumber(98));  
end.





(*
run:

25 square = 625
TRUE
5 square = 25
TRUE
76 square = 5776
TRUE
98 square = 9604
FALSE
376 square = 141376
TRUE

*)





answered Dec 9, 2025 by avibootz
...