Page 1 of 1

COBOL : The Fibonacci Sequence

#1 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 546
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Posted 02 May 2011 - 05:59 AM

COBOL : The Fibonacci Sequence


This tutorial introduces COBOL to you, in the form of a program that produces the Fibonacci sequence (I have got bored with "Hello World" tutorials :sarcasm:).

I have used the OpenCobol compiler on Linux.

So, without further ado, let's get started.

Divisions and Sections


The IDENTIFICATION DIVISION is used to declare the program identification and is the only division that cannot be further sub-divided into SECTION's. Even the program identification is somewhat redundant, but still needs to be specified to successfully compile a program.

The ENVIRONMENT DIVISION is used to provide information pertaining to the environment in which the program will run and consists of two sections, the CONFIGURATION SECTION and the INPUT-OUTPUT SECTION. You will notice that in the code below, neither of these sections is specified as there is no need for them within the context of this program.

The DATA DIVISION is where we specify data items. This division consists of six sections, the FILE SECTION, the WORKING-STORAGE SECTION, the LOCAL-STORAGE SECTION, the LINKAGE SECTION, the REPORT SECTION and the SCREEN SECTION. If there are no entries for a section, the section name can be omitted. In the tutorial example, the only items that need to be declared are WORKING-STORAGE SECTION items, so that is the only section declared.

Within the WORKING-STORAGE section we have a number of declarations.

Each line specifies a level number, an identifier and a usage. For example, the statement

       01  ix                    BINARY-C-LONG VALUE 0.

specifies a top-level field called ix, that is held as a 32-bit binary value and has an initial value of zero.

The power of COBOL starts becoming apparent when we look at the statement

       01  display-number        PIC Z(19)9.

This specifies a top-level field called display-number that has a picture clause which consists of 20 digits. If any of the leading digits are zero, then display them as spaces. Every time a value is moved into this field, this editing is honored.

The PROCEDURE DIVISION consists not of sections, but of labels and procedural statements (remember COBOL is not an object-oriented language but does support structured statements so that the GO TO statement only needs to be used on rare occasions.

The beauty of this language is that it is in English. The tutorial does not for example need to explain to you that the statement

ADD first-number TO second-number GIVING temp-number

add's the first-number data field to the second-number data field to give you a result placed into temp-number!

           PROGRAM-ID. "Fibonacci".
       01  ix                    BINARY-C-LONG VALUE 0.
       01  first-number          BINARY-C-LONG VALUE 0.
       01  second-number         BINARY-C-LONG VALUE 1.
       01  temp-number           BINARY-C-LONG VALUE 1.
       01  display-number        PIC Z(19)9.
      * This is the start of the program
           MOVE first-number TO display-number.
           DISPLAY display-number.
           MOVE second-number TO display-number.
           DISPLAY display-number.
           PERFORM VARYING ix FROM 1 BY 1 UNTIL ix = 90
               ADD first-number TO second-number GIVING temp-number
               MOVE second-number TO first-number
               MOVE temp-number TO second-number
               MOVE temp-number TO display-number
               DISPLAY display-number
           STOP RUN.

This post has been edited by Martyn.Rae: 02 May 2011 - 06:00 AM

Is This A Good Question/Topic? 1
  • +

Replies To: COBOL : The Fibonacci Sequence

#2 nathanpc   User is offline

  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Posted 02 May 2011 - 07:54 AM

OH MY GOD! Cobol!! *runs*
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon

Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Posted 02 May 2011 - 08:53 AM

We're actually still using COBOL on the mainframe at work. I'm amazed at how deeply embedded this language has managed to become in the years it's been around.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1