Home » » Beberapa Contoh Program Sederhana Pascal

Beberapa Contoh Program Sederhana Pascal


Di bawah ini ada beberapa contoh program sederhana yang bisa kita buat melalui bahasa pascal.

1. Program Bintang Piramid

program bintang_piramid;
uses crt;

var c,a,b,n : integer;
begin
write ('masukkan angka : ');readln(n);
writeln;
if n mod 2 = 0 then
for a := 1 to (n div 2) do begin
for b := 1 to (n div 2)-a do
write (‘ ‘);
for c := 1 to (a*2) do
write (‘*’);
writeln;
end
else
for a := 1 to (n div 2)+1 do begin
for b := 1 to (n div 2)+(1-a) do
write (‘ ‘);
for c := 1 to (a*2)-1 do
write (‘*’);
writeln;
end;
end.

2. Program Kalkulator Sederhana

USES CRT;
var a,b,c : real;
    pilih : char;
begin
clrscr;
writeln('------------------------------------------');
write('masukan angka pertama: '); readln(a);
write('silahkan pilih operator +,/,-,* :  '); readln(pilih);
write('masukan angka kedua  : '); readln(b);
if(pilih='+') then begin c:= a+b end;
if(pilih='/') then begin c:= a/b end;
if(pilih='-') then begin c:= a-b end;
if(pilih='*') then begin c:= a*b end;
writeln('Hasilnya adalah:  ',c:0:2);
writeln('  ');
writeln('**********KALKULATOR*************');
writeln('        Angka kedua  : ',a:0:2);
writeln('        Operator     : ',pilih);
writeln('        Angka kedua  : ',b:0:2);
writeln('Maka hasilnya adalah : ',c:0:2);
READKEY;
end.


3. Program Antrian

uses crt;
const
 nmax = 20;
 null = 0;

type
 tipedata =string;
 queue = record
          tabelemen : array[1..nmax] of tipedata;
          front : null..nmax;
          rear : null..nmax;
          data : array[1..nmax] of tipedata;
         end;

var
 N,i : integer;
 data : array[1..nmax] of tipedata;
 Q : queue;
 pilih : char;
procedure inisialisasi (var Q : queue);
begin
 Q.front := null;
 Q.rear := null;
end;

function emptyQ(Q:queue) : boolean;
begin
 emptyQ := ((Q.front = null) and (Q.rear=null));
end;

procedure consolidate(var Q : queue);
var i,j : integer;
begin
 j := 1;
 for i := Q.front to Q.rear do
 begin
 Q.tabelemen[j] := Q.tabelemen[i];
 j:= j+1;
 end;
 Q.front := 1;
 Q.rear := j;
 end;

procedure enqueue(var Q : queue;data : tipedata);
begin
 if emptyQ(Q) then
  Q.front := 1;
  if Q.rear <> nmax then
  begin
  Q.rear := Q.rear + 1;
  Q.tabelemen[Q.rear] := data;
  end
  else
  if Q.front <> 1 then
  begin
   consolidate(Q);
   Q.rear := Q.rear + 1;
   Q.tabelemen[Q.rear] := data;
  end;
 end;

procedure dequeue(var Q : queue; var data : tipedata);
begin
 if not emptyQ(Q) then
 begin
 data := Q.tabelemen[Q.front];
 Q.front := Q.front + 1;
 if (Q.front>Q.rear) then
 begin
  Q.front := null;
  Q.rear := null;
 end
 else
  data := Q.tabelemen[Q.front];
end;
end;


begin
 clrscr;
 inisialisasi(Q);
 emptyQ(Q);
 write('Masukkan jumlah pasien :');
 readln(N);
 for i := 1 to N do
 begin
  write('pasien ke ',i,':');
  readln(Q.data[i]);
 end;
 writeln;
 writeln;
 write('Daftar Antrian : ');
 for i := 1 to N do
  begin
  write(Q.data[i],'->');
  end;
  readkey;
end.
Thanks for reading Beberapa Contoh Program Sederhana Pascal

« Previous
« Prev Post
Next »
Next Post »

0 komentar:

Posting Komentar