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.

3 comments:

  1. Casino, Table Games, Poker, Blackjack, and more - Dr.MCD
    The online gambling industry 파주 출장샵 is booming. 창원 출장샵 and you will soon find casino games to 여주 출장안마 play with, whether you are 공주 출장샵 playing in online 경주 출장샵 poker rooms or in

    ReplyDelete
  2. The Best Slots | Casino Roll
    The nba매니아 best slots at Casino Roll. If you love 바카라 사이트 table games, to play 바카라 사이트 blackjack, you have to bet twice gri-go.com for the dealer to win. The dealer must

    ReplyDelete
  3. This technique of producing makes use of pc applications to regulate machine instruments that create close-tolerance high-quality parts. The technique of producing frees up manpower for added tasks whereas allowing for exact results on a repeatable basis. Direct CNC The measurement and variety of segments are determined by the accuracy required and the method chosen, and will immediately affect the execution.

    ReplyDelete