Friday, December 18, 2020

Flow Chart and Pascal Program for Fibonacci Series

Draw a flow chart which generates Fibonacci series which  element's value is less than 100


Flow chart and Pascal Program for Fibonacci














Write a Pascal Program to generate Fibonacci series which  element's value is less than 100

program FibonacciSeries;

var
  previous, beforeprevious,current:integer;

begin
  writeln(chr(13));
  write('First 100 items of the Fibonacci series...');
  writeln(chr(13));
  write('------------------------------------------');
  writeln(chr(13));
  writeln(chr(13));
  current:=1;
  previous:=1;
  while (current <=100) do
  begin
    write(current);
    write(Chr(9));
    beforeprevious:=previous;
    previous:=current;
    current:= beforeprevious+previous;
  end;
  readln;
end.    

Note:
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
Fn=Fn-1 +Fn-2
For n>1

Then the beginning of the sequence is :
0,1,1,2,3,5,8,13,21,34,55,89,144...

In some books, the value F0=0 is omitted, so that the sequence starts with F1=F2=1 and the recurrence
Fn=Fn-1+Fn-2
For n>2
1,1,2,3,5,8,13,21,34,55,89,144...


Pascal Program to Print Sequence 0,1,1,2,3,5,8,13,21,34,55,89... is given below.

program FibonacciSeries;

var
  previous, beforeprevious,current:integer;

begin
  writeln(chr(13));
  write('First 100 items of the Fibonacci series...');
  writeln(chr(13));
  write('------------------------------------------');
  writeln(chr(13));
  writeln(chr(13));
  current:=0;
  previous:=1;
  while (current  <=100) do
  begin
    write(current);
    write(Chr(9));
    beforeprevious:=previous;
    previous:=current;
    current:= beforeprevious+previous;
  end;
  readln;
end.             

Printing Fibonacci series using a recursive function

program recursiveFibonacci;
var
   i,n: integer;
function fibonacci(n: integer): integer;

begin
   if n=1 then
      fibonacci := 0

   else if n=2 then
      fibonacci := 1

   else
      fibonacci := fibonacci(n-1) + fibonacci(n-2);
end;

begin
   i:=1 ;
   while(n<=100) do
   begin
        n:= fibonacci (i);
        if n > 100 then
           break;
        write(n, '  ');
        i:=i+1;
   end;

   readln;
end.

No comments:

Post a Comment