Page 1 of 1

PASCAL : The Fibonacci Sequence

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Posted 07 May 2011 - 08:58 AM

PASCAL : The Fibonacci Sequence

Introduction

PASCAL is a programming language developed in the very early '70s, and although was initially aimed as a teaching aid for structured programming, has acquired a firm footing in the industry.

The basic structure of a PASCAL program is as follows:

PROGRAM Name;
CONST
(* Constant declarations *) 
TYPE
(* Type declarations *)
VAR
(* Variable declarations *)
(* Subprogram definitions *)
BEGIN
(* Statements *)
END.



Comments are expressed as anything between (* and *), and you cannot nest comments in a similar way to not being able to nest comments in C/C++.

The Code

The following code has been compiled and tested using the free pascal compiler found here.

PROGRAM Fibonacci;
VAR
   First, Second, Temp : int64;
   ix : integer;
BEGIN
   First := 0;
   Second := 1;
   writeln(First);
   writeln(Second);
   for ix := 0 to 90 do
   begin
      Temp := FIrst + Second;
      First := Second;
      Second := Temp;
      writeln(Temp);
   end
END.



The first line of the code above states that the program is known as Fibonacci. This name does not have to match the name of the file in which it is placed, as the filename is ultimately what the executable is called, not this name (consider it as an internal reference name).

Next we declare the variables for the program. I have declared First, Second and Temp as 64-bit wide integers and ix as a 32-bit integer.

Finally, to the executable statements which sit between the BEGIN and the END.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1