-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
cobol.sample
94 lines (74 loc) · 2.54 KB
/
cobol.sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
******************************************************************
* Author: Bryan Flood
* Date: 25/10/2018
* Purpose: Compute Fibonacci Numbers
* Tectonics: cobc
******************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. FIB.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 N0 BINARY-C-LONG VALUE 0.
01 N1 BINARY-C-LONG VALUE 1.
01 SWAP BINARY-C-LONG VALUE 1.
01 RESULT PIC Z(20)9.
01 I BINARY-C-LONG VALUE 0.
01 I-MAX BINARY-C-LONG VALUE 0.
01 LARGEST-N BINARY-C-LONG VALUE 92.
PROCEDURE DIVISION.
*> THIS IS WHERE THE LABELS GET CALLED
PERFORM MAIN
PERFORM ENDFIB
GOBACK.
*> THIS ACCEPTS INPUT AND DETERMINES THE OUTPUT USING A EVAL STMT
MAIN.
DISPLAY "ENTER N TO GENERATE THE FIBONACCI SEQUENCE"
ACCEPT I-MAX.
EVALUATE TRUE
WHEN I-MAX > LARGEST-N
PERFORM INVALIDN
WHEN I-MAX > 2
PERFORM CASEGREATERTHAN2
WHEN I-MAX = 2
PERFORM CASE2
WHEN I-MAX = 1
PERFORM CASE1
WHEN I-MAX = 0
PERFORM CASE0
WHEN OTHER
PERFORM INVALIDN
END-EVALUATE.
STOP RUN.
*> THE CASE FOR WHEN N = 0
CASE0.
MOVE N0 TO RESULT.
DISPLAY RESULT.
*> THE CASE FOR WHEN N = 1
CASE1.
PERFORM CASE0
MOVE N1 TO RESULT.
DISPLAY RESULT.
*> THE CASE FOR WHEN N = 2
CASE2.
PERFORM CASE1
MOVE N1 TO RESULT.
DISPLAY RESULT.
*> THE CASE FOR WHEN N > 2
CASEGREATERTHAN2.
PERFORM CASE1
PERFORM VARYING I FROM 1 BY 1 UNTIL I = I-MAX
ADD N0 TO N1 GIVING SWAP
MOVE N1 TO N0
MOVE SWAP TO N1
MOVE SWAP TO RESULT
DISPLAY RESULT
END-PERFORM.
*> PROVIDE ERROR FOR INVALID INPUT
INVALIDN.
DISPLAY 'INVALID N VALUE. THE PROGRAM WILL NOW END'.
*> END THE PROGRAM WITH A MESSAGE
ENDFIB.
DISPLAY "THE PROGRAM HAS COMPLETED AND WILL NOW END".
END PROGRAM FIB.
*> From https://github.com/KnowledgePending/COBOL-Fibonacci-Sequence/blob/master/fib.cbl