Example program fibonacci with pascal :
Example 1 :
program Fibonacci; var i, x, m, n : integer; begin {Begins the main part} writeln ('Enter the lenghts of the sequence'); readln (x); n := 0; m:=1; {We have to initialize our sequence} writeln (n); {and have the first output} writeln (m); i:=0; {"i" is the number of the current iteration} while i<x-2 do {The first 2 "x's" we've already had that's why we must get rid of them} begin {Begins the iteration loop} i:=i+1; m:=m+n; n:=m-n; writeln(m); if i=x-2 then writeln('Finished !'); {The program has calculated everything and lets us know about it} end; {The end of the while-do loop} readln; {This is needed to have enough time to read the output} end.
Example 2 :
program fibonacci;
function fib(n:integer): integer;
begin
if (n <= 2) then
fib := 1
else
fib := fib(n-1) + fib(n-2);
end;
var
i:integer;
begin
for i := 1 to 16 do
write(fib(i), ', ');
writeln('...');
end.
Example 3 :
program bilangan_fibonacci;
{menampilakan bilangan fibonacci kurang dari 100}
uses wincrt;
var
x : array [1..11] of integer;
i : integer;
begin
clrscr;
x[1] := 1;
x[2] := 1;
write (x[1], ' ');
write (x[2], ' ');
for i := 3 to 11 do
begin
x[i] := x[i - 1] + x[i - 2];
write (x[i], ' ');
end;
end.
Example 4 :
PROGRAM TestFib;
USES Fib_I, Fib_R, Fib_D;
VAR
I: INTEGER;
BEGIN
WRITELN ('Fibonacci function test program version Pascal 1');
WRITELN ('Enter a negative number to quit');
WRITE ('I? ');
READ (I);
WHILE I >= 0 DO BEGIN
WRITELN ('Dynamic: ', Fib_D.F (I));
WRITELN ('Iterative: ', Fib_I.F (I));
WRITELN ('Recursive: ', Fib_R.F (I));
WRITE ('I? ');
READ (I);
END
END.






0 comments:
Posting Komentar