program seling;
uses wincrt;
var i,j,n:integer;
a:string;
begin
write ('banyak baris ');
readln (n);
for i:=1 to n do
begin
for j:=1 to i do
begin
if (j mod 2)=1 then
a:='*' else a:='1';
write(a,' ');
end;
writeln;end;
end.
CONTOH PROGRAM BENTUK BINTANG LUAS SEGI EMPAT
program persegi;
uses winCrt;
procedure gb (brs,kol:integer);
var i,j:integer;
begin
for i:=1 to brs do
begin
for j:=1 to kol do
begin
if ((i=1)or(i=brs)or(j=1)or(j=kol))then
write ('*')
else write(‘ ‘);
end;
writeln;
end;
end;
var x,y:integer;
begin
write('banyak baris= ');readln(y);
write('banyak kolom= ');readln(x);
writeln('bentuknya:');
gb(y,x);
end.
Output:
Banyaknya baris 3
Banyaknya kolom 4
****
* *
****
Contoh matrik penjumlahan, pengurangan dan perkalian:
program matrik_kali;
uses wincrt;
var a,b,c: array[1..10,1..10] of real;
i,j,k,baris1,kolom1,baris2,kolom2:integer;
begin
writeln('ukuran matrik A ');
read(baris1,kolom1);
write('masukan nilai matrik');
for i:=1 to baris1 do
for j:=1 to kolom1 do
read(a[i,j]);
write('ukuran matrik B ');
read(baris2,kolom2);
writeln('masukan nilai matrik');
for i:=1 to baris2 do
for j:=1 to kolom2 do
read(b[i,j]);
if kolom1=baris2 then
for i:=1 to baris1 do
for j:=1 to kolom2 do
begin
c[i,j]:=0;
for k:=1 to kolom1 do
c[i,j]:=c[i,j]+a[i,k]*b[k,j];
end
else
write('ukuran matrix tidak sesuai syarat');
writeln('hasil perkalian');
for i:=1 to baris1 do
begin
for j:=1 to kolom2 do
write(c[i,j]:0:0,' ');
writeln
end;
begin if (baris1=baris2) and (kolom1=kolom2) then
begin
for i:=1 to baris2 do
for j:=1 to kolom2 do
c[i,j]:= a[i,j]+b[i,j]
end
else
writeln('ukuran matrik tidak sama');
writeln('hasil penjumlahan');
for i:=1 to baris1 do
begin
for j:=1 to kolom1 do
write(c[i,j]:0:0,' ');
writeln;
end;
end;
begin if (baris1=baris2) and (kolom1=kolom2) then
begin
for i:=1 to baris2 do
for j:=1 to kolom2 do
c[i,j]:= a[i,j]-b[i,j]
end
else
writeln('ukuran matrik tidak sama');
writeln('hasil pengurangan');
for i:=1 to baris1 do
begin
for j:=1 to kolom1 do
write(c[i,j]:0:0,' ');
writeln;
end; end; end.
0 komentar:
Posting Komentar