EXEMPLOS DE ALGORITMOS NA LINGUAGEM PASCAL
Autor: Elisson Mattos | Publicado: 2015-02-17T11:35:00-08:00


A seguir alguns exemplos simples de algoritmos na linguagem pascal



1. Calcule a média aritmética de 10 números reais lidos.



Program MD_NUM;

uses crt;

var num, cont: integer;

soma, media: real;

begin&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clrscr;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont:=1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; soma:=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while cont &lt;= 10 do

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;write ('Digite o',cont,'o. número:');

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readln (num);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; soma:= num + soma;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont:= cont + 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;&nbsp;





&nbsp; &nbsp; &nbsp; &nbsp; media:=soma/10;

&nbsp; &nbsp; &nbsp; &nbsp; writeln ('A media dos números é ',soma:3:2);

&nbsp; &nbsp; &nbsp; &nbsp; write ('Tecle ALGO para sair');

&nbsp; &nbsp; &nbsp; &nbsp; readkey;

end.







2. Calcular a média aritmética de N números lidos somando somente os ímpares.



Program MD_IMPAR;

uses crt;

var n, num, cont, ci: integer;

si: real;

begin

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clrscr;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;write ('Digite a quantidade de números: ');

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readln (n);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (n &gt; 0) Then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cont:= 1; ci:= 0; si:= 0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (cont &lt;= n) do

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write('Digite o ',cont, 'o. número');

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readln (num);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if num mod 2&lt;&gt;0 then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;si:= si + num;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ci:= ci + 1 ;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cont:= cont + 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ci &gt; 0 then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writeln ('A media dos',ci,'o. num impares lidos é ',si / ci: 4: 2);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writeln ('Não foi lido nenhum numero impar');

&nbsp; &nbsp; &nbsp; &nbsp;end

&nbsp; &nbsp; &nbsp; &nbsp;else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeln ('A quantidade precisar ser maior que zero');

&nbsp; &nbsp; &nbsp; &nbsp;write ('Tecle ALGO para sair');

&nbsp; &nbsp; &nbsp; &nbsp;readkey;

end.