Program Pascal - Deret Bilangan Segitiga & Fibonacci Menggunakan Fungsi Rekursif
Source Code:
program Der_Bil_Sgtg_Fibo_REK;
uses crt;
var pil:char;
function fibonacci(x:integer):integer;
begin
if(x=1) then fibonacci:=0 else
if(x=2) then fibonacci:=1 else
fibonacci:=fibonacci(x-1)+fibonacci(x-2);
end;
function segitiga (n:integer):real;
var i:integer;
begin
if n > 0 then
begin
segitiga:= segitiga(n-1)+n;
end;
end;
procedure menu1;
var n,i: integer;
begin
clrscr;
writeln;
writeln('>> Deret Bilangan Segitiga <<');
writeln;
write(' Input Bilangan : '); readln(n);
writeln;
write(' Hasil : ');
for i:=1 to n do
write(segitiga(i):2:0,' ');
readkey;
end;
procedure menu2;
var n,i : integer;
begin
clrscr;
writeln;
writeln('>> Deret Bilangan Fibonacci <<');
writeln;
write (' Masukkan Banyak Deret = ');readln(n);
writeln;
writeln(' Hasilnya : ');
writeln;
for i:=1 to n do
write(' ',fibonacci(i),' ');
readkey;
end;
begin
repeat
clrscr;
writeln;
writeln('>> PROGRAM BIL. SEGITIGA & FIBONACCI REKURSIF <<');
writeln;
writeln(' ------>>>>> Menu Utama <<<<<-------- ');
writeln;
writeln(' [1] Bilangan Segitiga');
writeln(' [2] Bilangan Fibonacci');
writeln(' [0] Exit Program');
writeln;
write ('Pilihan : ');pil:=readkey;
case pil of
'1':menu1;
'2':menu2;
end;
until
pil='0';
end.
Posting Komentar