Just Share Information and Experience

Wednesday, 28 October 2015

Membuat deret fibonacci di pascal

program fibonaci;
{
  Menampilkan deret fibonaci
  Masukkan : User memasukkan jumlah deret fibonaci
  Keluaran : Menampilkan deret fibonaci sebanyak data yang di masukkan user
}

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };
var
  data,i,nilai,nilai1,nilai2 : integer;

begin
    write('Masukkan banyak deret : '); readln(data);

    writeln;
    writeln('Deret fibonaci dengan ',data,' deret pertama! ');
    nilai1:=2;
    nilai:=1;
    nilai2:=0;

    writeln;

    for i:=1 to data do
    begin
      if(i=1) or (i=2) then
         begin

         write(nilai,' ');
         end
      else
        begin

        write(nilai1,' ');
        nilai2:=nilai1;
        nilai1:=nilai1+nilai;

        nilai :=nilai2;
        end;

    end;
    readln;
end.

No comments:

Post a Comment