Blob Blame Raw
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Math;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  num: integer;
  n1, n2, n3, n4, n5, n6, sum1, sum2: integer;
begin
  num := StrToInt(Edit1.Text);

  n1 := num mod 10;
  n2 := (num div 10) mod 10;
  n3 := (num div 100) mod 10;
  n4 := (num div 1000) mod 10;
  n5 := (num div 10000) mod 10;
  n6 := num div 100000;

  sum1 := n1 + n2 + n3;
  sum2 := n4 + n5 + n6;

  if sum1 = sum2 then
    Label1.Caption := 'Счастливый'
  else
    Label1.Caption := 'Обычный';
end;

// extract digit from number
function digit(num, pos: integer): integer;
var
  x: integer;
begin
  x := round(Power(10, pos));
  Result := (num div x) mod 10;
end;

// here, for new experience, we will
// extract digits from integer instead of string
procedure TForm1.Button2Click(Sender: TObject);
var
  num: integer;
  i, sum1, sum2: integer;
begin
  num := StrToInt(Edit1.Text);

  sum1 := 0;
  sum2 := 0;

  for i := 0 to 2 do begin
    sum1 := sum1 + digit(num, i);
    sum2 := sum2 + digit(num, i+3);
  end;

  if sum1 = sum2 then
    Label1.Caption := 'Счастливый'
  else
    Label1.Caption := 'Обычный';
end;

end.