Fórum funçao para fatorial... #167365
02/07/2003
0
OBRIGADO
function fatorial(x:integer): integer;
var
f, i:integer;
begin
f:=1;
for I:=1 to X do
f:=f *i;
fatorial :=f;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
A: array[1..5] of Integer;
n,I: Integer;
begin
A[1] := StrToInt(Edit1.Text);
A[2] := StrToInt(Edit2.Text);
A[3] := StrToInt(Edit3.Text);
A[4] := StrToInt(Edit4.Text);
A[5] := StrToInt(Edit5.Text);
n := A[I];
Label2.Caption := IntToStr(fatorial(n));
Edit1.SetFocus;
end;
Harry
Curtir tópico
+ 0Posts
02/07/2003
Fred
var i, r : integer
begin
r := 0;
for i := 1 to 5 do
begin
r := r*a[i];
end;
result := r;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
A: array[1..5] of Integer;
begin
A[1] := StrToInt(Edit1.Text);
A[2] := StrToInt(Edit2.Text);
A[3] := StrToInt(Edit3.Text);
A[4] := StrToInt(Edit4.Text);
A[5] := StrToInt(Edit5.Text);
Label2.Caption := IntToStr(fatorial(A));
Edit1.SetFocus;
end;
tente isto. 8)
Gostei + 0
02/07/2003
Jairroberto
Não entendi muito bem o que você quer, pois há apenas um label mostrando o resultado e são 5 elementos no vetor para calcular o fatorial. Porém, foi possível identificar um erro no seguinte código:
procedure TForm1.Button1Click(Sender: TObject); var A: array[1..5] of Integer; n,I: Integer; begin A[1] := StrToInt(Edit1.Text); A[2] := StrToInt(Edit2.Text); A[3] := StrToInt(Edit3.Text); A[4] := StrToInt(Edit4.Text); A[5] := StrToInt(Edit5.Text); n := A[I]; // neste ponto I vale zero, por isso não há resultado Label2.Caption := IntToStr(fatorial(n)); Edit1.SetFocus; end;
Você poderia alterar o código para:
procedure TForm1.Button1Click(Sender: TObject); var A: array[1..5] of Integer; n,I: Integer; begin A[1] := StrToInt(Edit1.Text); A[2] := StrToInt(Edit2.Text); A[3] := StrToInt(Edit3.Text); A[4] := StrToInt(Edit4.Text); A[5] := StrToInt(Edit5.Text); for I := Low(A) to High(A) do begin n := A[I]; // agora I varia de 1 a 5 // a seguir, cada Label (Label1, Label2, ...) mostra o fatorial de cada elemento do vetor TLabel(FindComponent(Format(´Label¬d´, [I]))).Caption := IntToStr(fatorial(n)); end; Edit1.SetFocus; end;
Um abraço,
Jair
Gostei + 0
02/07/2003
Marconi
var i, r : integer
begin
if X<3 then result:=X; //
else begin
r := 2;
for i := 3 to X do begin
r := r*i;
end;
end;
result := r;
end;
{ se voce deixar o r inicial = 0, por mais que multiplique vai dar sempre zero}
Gostei + 0
02/07/2003
Marconi
Quando voce fez a chamada seguinte
n := A[I];
Label2.Caption := IntToStr(fatorial(n));
Voce não tinha dado nenhum valor para I ainda. Ele ainda esta com o valor NULL, e vai dar erro na certa.
Marconi
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)