Fibonacci numbers in COBOL

Example for versions OpenCOBOL 1.0, TinyCOBOL 0.65.9

This example uses iterative calculation of Fibonacci numbers. Two numbers are added using command ADD which puts the sum of two arguments in the third one. DISPLAY adds a newline after each item it prints, so all numbers have to be concatenated in one string before being printed. To do this, STRING command is used. For each item concatenated one has to set DELIMITED BY option: SIZE — all variable is used (declared size of it), SPACE — the part of the variable till first whitespace. This means that the concatenated string can’t contain spaces, and the output looks like this:

001,001,002,003,005,008,013,021,034,055,089,144,233,377,610,987,...

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SAMPLE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

         77 fib1 pic 999.
         77 fib2 pic 999.
         77 fib3 pic 999.
         77 i pic 99.
         77 fibst pic XXX.
         77 res pic X(64).

       PROCEDURE DIVISION.
         move 0 to i
         move 0 to fib1
         move 1 to fib2
         move "" to res
         perform until i greater than 15
           add fib1 to fib2 giving fib3
           move fib2 to fib1
           move fib3 to fib2
           move fib1 to fibst
           string res   DELIMITED BY SPACE
                  fibst DELIMITED BY SIZE
                  ","   DELIMITED BY SIZE into res
           add 1 to i
         end-perform.
         display res "..."
         stop run.