Página 1 de 1

verifica se um número é primo em Delphi

Enviado: quarta mar 19, 2025 7:30 pm
por Keithclecy
unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
TForm1 = class(TForm)
EditNumero: TEdit;
ButtonVerificar: TButton;
LabelResultado: TLabel;
Label1: TLabel;
procedure ButtonVerificarClick(Sender: TObject);
private
public
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonVerificarClick(Sender: TObject);
var
Numero, i: Integer;
Primo: Boolean;
begin
Numero := StrToInt(EditNumero.Text);
Primo := True;

if Numero < 2 then
Primo := False
else
begin
for i := 2 to Trunc(Sqrt(Numero)) do
if Numero mod i = 0 then
begin
Primo := False;
Break;
end;
end;

if Primo then
LabelResultado.Caption := IntToStr(Numero) + ' é primo'
else
LabelResultado.Caption := IntToStr(Numero) + ' não é primo';
end;

end.