Blame lazarus/lucky-number/unit1.pas

0c1f07
unit Unit1;
0c1f07
0c1f07
{$mode objfpc}{$H+}
0c1f07
0c1f07
interface
0c1f07
0c1f07
uses
0c1f07
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
0c1f07
  Math;
0c1f07
0c1f07
type
0c1f07
0c1f07
  { TForm1 }
0c1f07
0c1f07
  TForm1 = class(TForm)
0c1f07
    Button1: TButton;
0c1f07
    Button2: TButton;
0c1f07
    Edit1: TEdit;
0c1f07
    Label1: TLabel;
0c1f07
    Label2: TLabel;
0c1f07
    procedure Button1Click(Sender: TObject);
0c1f07
    procedure Button2Click(Sender: TObject);
0c1f07
  private
0c1f07
    { private declarations }
0c1f07
  public
0c1f07
    { public declarations }
0c1f07
  end;
0c1f07
0c1f07
var
0c1f07
  Form1: TForm1;
0c1f07
0c1f07
implementation
0c1f07
0c1f07
{$R *.lfm}
0c1f07
0c1f07
procedure TForm1.Button1Click(Sender: TObject);
0c1f07
var
0c1f07
  num: integer;
0c1f07
  n1, n2, n3, n4, n5, n6, sum1, sum2: integer;
0c1f07
begin
0c1f07
  num := StrToInt(Edit1.Text);
0c1f07
0c1f07
  n1 := num mod 10;
0c1f07
  n2 := (num div 10) mod 10;
0c1f07
  n3 := (num div 100) mod 10;
0c1f07
  n4 := (num div 1000) mod 10;
0c1f07
  n5 := (num div 10000) mod 10;
0c1f07
  n6 := num div 100000;
0c1f07
0c1f07
  sum1 := n1 + n2 + n3;
0c1f07
  sum2 := n4 + n5 + n6;
0c1f07
0c1f07
  if sum1 = sum2 then
0c1f07
    Label1.Caption := 'Счастливый'
0c1f07
  else
0c1f07
    Label1.Caption := 'Обычный';
0c1f07
end;
0c1f07
0c1f07
// extract digit from number
0c1f07
function digit(num, pos: integer): integer;
0c1f07
var
0c1f07
  x: integer;
0c1f07
begin
0c1f07
  x := round(Power(10, pos));
0c1f07
  Result := (num div x) mod 10;
0c1f07
end;
0c1f07
0c1f07
// here, for new experience, we will
0c1f07
// extract digits from integer instead of string
0c1f07
procedure TForm1.Button2Click(Sender: TObject);
0c1f07
var
0c1f07
  num: integer;
0c1f07
  i, sum1, sum2: integer;
0c1f07
begin
0c1f07
  num := StrToInt(Edit1.Text);
0c1f07
0c1f07
  sum1 := 0;
0c1f07
  sum2 := 0;
0c1f07
0c1f07
  for i := 0 to 2 do begin
0c1f07
    sum1 := sum1 + digit(num, i);
0c1f07
    sum2 := sum2 + digit(num, i+3);
0c1f07
  end;
0c1f07
0c1f07
  if sum1 = sum2 then
0c1f07
    Label1.Caption := 'Счастливый'
0c1f07
  else
0c1f07
    Label1.Caption := 'Обычный';
0c1f07
end;
0c1f07
0c1f07
end.
0c1f07