Program Deret Fibonacci secara Rekursif dalam Bahasa Pascal
Program Deret_Fibonacci;
function Fibonacci(n:integer) : integer;
var
a, b, c, i : integer;
begin
if n = 0 then
begin
Fibonacci := 0;
end
else if n = 1 then
begin
Fibonacci := 1;
end
else
begin
a:=0;
b:=1;
i:=2;
while i<=n do
begin
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end;
Fibonacci := c;
end;
end;
var
i, n:integer;
begin
write('masukkan deret fibonacci : ');
readln(n);
writeln();
write('deret fibonacci : ');
begin
for i:= 0 to n-1 do
write(Fibonacci(i),' ');
end;
end.
function Fibonacci(n:integer) : integer;
var
a, b, c, i : integer;
begin
if n = 0 then
begin
Fibonacci := 0;
end
else if n = 1 then
begin
Fibonacci := 1;
end
else
begin
a:=0;
b:=1;
i:=2;
while i<=n do
begin
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end;
Fibonacci := c;
end;
end;
var
i, n:integer;
begin
write('masukkan deret fibonacci : ');
readln(n);
writeln();
write('deret fibonacci : ');
begin
for i:= 0 to n-1 do
write(Fibonacci(i),' ');
end;
end.
Comments
Post a Comment
-Berkomentarlah yang baik dan rapi.
-Menggunakan link aktif akan dihapus.