演算
加算 / 減算
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-DATA.
03 NUM1 PIC 9(5) VALUE 0.
03 NUM2 PIC 9(5) VALUE 0.
03 NUM3 PIC 9(5) VALUE 0.
03 NUM4 PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
MAIN SECTION.
ADD 5 TO NUM1 NUM2 NUM3 NUM4. *> 各変数に5を加算
ADD 5 TO NUM1 NUM2 NUM3 NUM4. *> 「ADD TO」 -> 加算
SUBTRACT 2 FROM NUM1 NUM2 *> 「SUBTRACT TO」 -> 減算
DISPLAY NUM-DATA.
STOP RUN.
実行
USER-no-MacBook-Air:COBOL user$ ./hello
00008000080001000010
乗算
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-DATA.
03 NUM1 PIC 9(5) VALUE 0.
03 NUM2 PIC 9(5) VALUE 0.
03 NUM3 PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
MAIN SECTION.
MOVE 11 TO NUM1.
MOVE 3 TO NUM2.
MULTIPLY NUM1 BY NUM2 GIVING NUM3 *> 「MULTIPLY BY」 -> 乗算
DISPLAY NUM-DATA. *> 「GIVING」 -> 結果を代入
STOP RUN. *> 11 * 3 = 33
実行
USER-no-MacBook-Air:COBOL user$ ./hello
000110000300033
除算
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-DATA.
03 NUM1 PIC 9(5) VALUE 0.
03 NUM2 PIC 9(5) VALUE 0.
03 NUM3 PIC 9(5) VALUE 0.
03 NUM4 PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
MAIN SECTION.
MOVE 11 TO NUM1.
MOVE 3 TO NUM2.
DIVIDE NUM1 BY NUM2 GIVING NUM3 REMAINDER NUM4. *> 「DIVDE BY」 -> 除算
DISPLAY NUM-DATA. *> 「GIVING」 -> 「DIVIDE BY」の結果を代入
STOP RUN. *> 「REMAINDER」 -> 余りを代入 *> 11 / 3 = 3 余り 2
実行
USER-no-MacBook-Air:COBOL user$ ./hello
00011000030000300002
COMPUTE を使う
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-DATA.
03 NUM1 PIC 9(5) VALUE 11.
03 NUM2 PIC 9(5) VALUE 2.
03 NUM3 PIC 9(5) VALUE 1.
PROCEDURE DIVISION.
MAIN SECTION. *> 「COMPUTE」 -> 四則演算が可能
COMPUTE NUM3 = NUM1 + NUM2. *> 加算
DISPLAY NUM-DATA.
COMPUTE NUM3 = NUM1 - NUM2. *> 減算
DISPLAY NUM-DATA.
COMPUTE NUM3 = NUM1 / NUM2. *> 除算
DISPLAY NUM-DATA.
COMPUTE NUM3 = NUM1 * NUM2. *> 乗算
DISPLAY NUM-DATA.
COMPUTE NUM3 = NUM1 ** NUM2. *> べき乗
DISPLAY NUM-DATA.
STOP RUN.
実行
USER-no-MacBook-Air:COBOL user$ ./hello
000110000200014
000110000200009
000110000200005
000110000200022
000110000200121