terça-feira, 11 de maio de 2010

Exemplo Fatorial

program fatpritab;
uses crt;
var
   f,i,c,n,r,op:integer;
   x:string[1];
   begin
   x:='s';
   while x='s' do
      begin
      textcolor(10);
      clrscr;
      writeln;
      writeln('MENU DE OPCOES:');
      writeln;
      writeln('1- Calcular o fatorial de um numero');
      writeln('2- Verificar se um numero eh primo');
      writeln('3- Gerar a tabuada para valores de -3 a 10');
      writeln;
      write('Digite a opcao escolhida = ');
      readln(op);
      if op=1 then
         begin
         clrscr;
         writeln;
         write('Digite um numero = ');
         readln(n);
         writeln;
         f:=1;
         for i:=n downto f do f:=f*i;
         writeln('O fatorial de ',n,' = ',f);
         writeln;
         write('Deseja executar novamente? s/n = ');
         x:=readkey;
         writeln(x);
         if x='s' then
            begin
            writeln;
            write('Reiniciando...');
            delay(1000);
            end
         else
            begin
            writeln;
            write('Saindo do programa...');
            delay(1000);
            end;
         end;
      if op=2 then
         begin
         clrscr;
         writeln;
         write('Digite um numero = ');
         readln(n);
         writeln;
         c:=0;
         for i:=1 to n do
            begin
            if n mod i=0 then c:=c+1;
            end;
         if c=2 then writeln('O numero ',n,' eh primo')
         else writeln('O numero ',n,' nao eh primo');
         writeln;
         write('Deseja executar novamente? s/n = ');
         x:=readkey;
         writeln(x);
         if x='s' then
            begin
            writeln;
            write('Reiniciando...');
            delay(1000);
            end
         else
            begin
            writeln;
            write('Saindo do programa...');
            delay(1000);
            end;
         end;
      if op=3 then
         begin
         clrscr;
         writeln;
         write('Digite um numero = ');
         readln(n);
         writeln;
         for i:=-3 to 10 do
            begin
            r:=i*n;
            writeln(i,' X ',n,' = ',r);
            end;
         writeln;
         write('Deseja executar novamente? s/n = ');
         x:=readkey;
         writeln(x);
         if x='s' then
            begin
            writeln;
            write('Reiniciando...');
            delay(1000);
            end
         else
            begin
            writeln;
            write('Saindo do programa...');
            delay(1000);
            end;
         end;
      end;
   end.

0 comentários: