Algorytmy

program nwd_ab_w(input,output);
{ program wykorzystuje instrukcje WHILE }
uses Crt;
var
a,b: integer;
begin
ClrScr;
writeln;
writeln('Algorytm
Euklidesa z uzyciem odejmowania');
writeln('=============================');
writeln;
writeln('Podaj wartosci liczb naturalnych a i b:');
writeln;
write(' a = ');readln(a);
write(' b = ');readln(b);
while a<>b do
if a>b then
a:= a-b
else
b:= b-a;
writeln;
writeln('Najwiekszy wspolny podzielnik liczb a i b wynosi ',a);
writeln;
writeln('Koniec obliczen')
end.

program nwd_ab_r(input,output);
{ program wykorzystuje instrukcje REPEAT }
uses Crt;
var
a,b,r: integer;
begin
ClrScr;
writeln;
writeln('Algorytm
Euklidesa z uzyciem dzielenia');
writeln('==========================');
writeln;
writeln('Podaj wartosci liczb naturalnych a i b:');
writeln;
write(' a = ');readln(a);
write(' b = ');readln(b);
repeat
r:= a MOD b;
a:= b;
b:= r
until r=0;
writeln;
writeln('Najwiekszy wspolny podzielnik liczb a i b wynosi ',a);
writeln;
writeln('Koniec obliczen')
end.

program suma_w(input,output);
{ program wykorzystuje instrukcje WHILE }
uses Crt;
var
l,n,s: integer;
begin
ClrScr;
writeln;
writeln('Obliczanie
sumy liczb naturalnych od 1 do n');
writeln('=============================');
writeln;
write('Podaj n = ');readln(n);
writeln;
s:= 0;
l:= n;
while l>0 do begin
s:= s+l;
l:= l-1
end;
writeln('Suma liczb
naturalnych od 1 do ',n,' wynosi ',s);
writeln;
writeln('Koniec obliczen')
end.

program suma_r(input,output);
{ program wykorzystuje instrukcje REPEAT }
uses Crt;
var
l,n,s: integer;
begin
ClrScr;
writeln;
writeln('Obliczanie
sumy liczb naturalnych od 1 do n');
writeln('=============================');
writeln;
write('Podaj n = ');readln(n);
writeln;
s:= 0;
l:= n;
repeat
s:= s+l;
l:= l-1
until l=0;
writeln('Suma liczb
naturalnych od 1 do ',n,' wynosi ',s);
writeln;
writeln('Koniec
obliczen')
end.

program suma_ft(input,output);
{ program wykorzystuje instrukcje FOR..TO }
uses Crt;
var
l,n,s: integer;
begin
ClrScr;
writeln;
writeln('Obliczanie
sumy liczb naturalnych od 1 do n');
writeln('=============================');
writeln;
write('Podaj n = ');readln(n);
writeln;
s:= 0;
for l:= 1 to n do s:= s+l;
writeln('Suma liczb
naturalnych od 1 do ',n,' wynosi ',s);
writeln;
writeln('Koniec obliczen')
end.

program suma_fd(input,output);
{ program wykorzystuje instrukcje FOR..DOWNTO }
uses Crt;
var
l,n,s: integer;
begin
ClrScr;
writeln;
writeln('Obliczanie
sumy liczb naturalnych od 1 do n');
writeln('====================================');
writeln;
write('Podaj n = ');readln(n);
writeln;
s:= 0;
for l:= n downto 1 do s:= s+l;
writeln('Suma liczb
naturalnych od 1 do ',n,' wynosi ',s);
writeln;
writeln('Koniec obliczen')
end.

program rown_1(input,output);
{ program
wykorzystuje instrukcje IF..THEN..ELSE }
uses Crt;
var
a,b,x: real;
begin
ClrScr;
writeln;
writeln('Rozwiazywanie
rownania stopnia pierwszego ax+b=0');
writeln('=====================================');
writeln;
write(' a = ');readln(a);
write(' b = ');readln(b);
if a=0 then
if b=0 then begin
writeln;
writeln('Rownanie
tozsamosciowe')
end
else begin
writeln;
writeln('Rownanie
sprzeczne')
end
else
begin
x:= -b/a;
writeln;
writeln('Rozwiazanie: x = ',x)
end;
writeln;
writeln('Koniec obliczen')
end.