## Tuesday, January 7, 2014

### Iterative solution to the Fibonacci sequence in COBOL

I've decided to learn cobol by contributing to Rosetta Code, a site that aims to show each as many different programming language's solution to a given problem.

Below is the iterative solution to the fibonacci sequence, which takes in input from the user's standard input keyboard.

View on Rosetta Code

```Program-ID. Fibonacci-Sequence.
Data Division.
Working-Storage Section.
01  FIBONACCI-PROCESSING.
05  FIBONACCI-NUMBER  PIC 9(36)   VALUE 0.
05  FIB-ONE           PIC 9(36)   VALUE 0.
05  FIB-TWO           PIC 9(36)   VALUE 1.
01  DESIRED-COUNT       PIC 9(4).
01  FORMATTING.
05  INTERM-RESULT     PIC Z(35)9.
05  FORMATTED-RESULT  PIC X(36).
05  FORMATTED-SPACE   PIC x(35).
Procedure Division.
000-START-PROGRAM.
Display "What place of the Fibonacci Sequence would you like (<173)? " with no advancing.
Accept DESIRED-COUNT.
If DESIRED-COUNT is less than 1
Stop run.
If DESIRED-COUNT is less than 2
Move FIBONACCI-NUMBER to INTERM-RESULT
Move INTERM-RESULT to FORMATTED-RESULT
Unstring FORMATTED-RESULT delimited by all spaces into FORMATTED-SPACE,FORMATTED-RESULT
Display FORMATTED-RESULT
Stop run.
Subtract 1 from DESIRED-COUNT.
Move FIBONACCI-NUMBER to INTERM-RESULT.
Move INTERM-RESULT to FORMATTED-RESULT.
Unstring FORMATTED-RESULT delimited by all spaces into FORMATTED-SPACE,FORMATTED-RESULT.
Display FORMATTED-RESULT.
Perform 100-COMPUTE-FIBONACCI until DESIRED-COUNT = zero.
Stop run.
100-COMPUTE-FIBONACCI.
Compute FIBONACCI-NUMBER = FIB-ONE + FIB-TWO.
Move FIB-TWO to FIB-ONE.
Move FIBONACCI-NUMBER to FIB-TWO.
Subtract 1 from DESIRED-COUNT.
Move FIBONACCI-NUMBER to INTERM-RESULT.
Move INTERM-RESULT to FORMATTED-RESULT.
Unstring FORMATTED-RESULT delimited by all spaces into FORMATTED-SPACE,FORMATTED-RESULT.
Display FORMATTED-RESULT.```