jueves, 29 de abril de 2021

Diversas maneras de calcular el factorial de un número entero no negativo

program factorial_1;
uses crt;

var x,num,resultat:integer;

begin
  repeat
    clrscr;
    write('entra un nombre enter ');readln(num);

    x:=num;
    resultat:=1;

    while (x<>1) do
      begin
        resultat:=resultat*x;
        x:=x-1;
      end;

    writeln('el factorial de ',num,'  ‚s  ',resultat);

    delay(1000);
  until keypressed;

end.

program factorial_2;
uses crt;

var x,num:integer;
var resultat:longint;

begin
  repeat
    clrscr;
    write('entra un nombre enter ');readln(num);

    x:=num;
    resultat:=1;

    for x:=num downto 1 do
      begin
        resultat:=resultat*x;
      end;

    writeln('el factorial de ',num,'  ‚s  ',resultat);

    delay(1000);
  until keypressed;

end.

program factorial_3;
uses crt;

var x,num,resultat:integer;

procedure fes;
begin
  while (x<>1) do
    begin
      resultat:=resultat*x;
      x:=x-1;
      fes;
    end;
end;


begin
  repeat
    clrscr;
    write('entra un nombre enter ');readln(num);
    x:=num;
    resultat:=1;
    fes;
    writeln('el factorial de ',num,'  ‚s  ',resultat);
    delay(2000);
  until keypressed;
end.

No hay comentarios:

Publicar un comentario