Search:

Submit Article

# Evaluates an arithmetic expression and stores the result

Posted By: Tyler Evans     Category: Cobol     Views: 5851

## Code for Evaluates an arithmetic expression and stores the result in Cobol

```IDENTIFICATION DIVISION.
PROGRAM-ID. TEST_COMPUTE.

DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM-01                   PIC S9(8)            VALUE 223344.
77 NUM-02                   PIC S9(3)V9(2)       VALUE  64.2.
77 NUM-03                   PIC S9(11)V9(3) COMP VALUE 876543.67.
77 NUM-04                   PIC S9(10)V9(4) COMP VALUE 100000.2222.
77 NUM-05                   PIC S9(9)V9(5)  COMP VALUE 200000.33333.

77 N1                       PIC S9(5)V9(3) COMP.
77 N2                       PIC S9(5)V9(2).
77 N3                       PIC S9(5)V9(4) COMP.
77 N4                       PIC S9(5)V9(4) COMP.
77 N5                       PIC S9(5)V9(2).
77 N6                       PIC S9(8).
77 N7                       PIC S9(5)V9(3) COMP.
77 N8                       PIC S9(5) COMP.

77 EDI-01                   PIC --------9.999.

PROCEDURE DIVISION.
MAIN.

*   COMPUTE res = ope1
DISPLAY "12345.678 = " WITH NO ADVANCING
COMPUTE NUM-03 = 12345.678
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 + ope2
DISPLAY "23.8765432 + 120026.01 = " WITH NO ADVANCING
COMPUTE NUM-03 = 23.8765432 + 120026.01.
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 * ope2 + ope3
DISPLAY "23 * 2 + 26 = " WITH NO ADVANCING
COMPUTE NUM-03 = 23 * 2 + 26
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 + ope2 * ope3
DISPLAY "23 + 26 * 2 = " WITH NO ADVANCING
COMPUTE NUM-03 = 23 + 26 * 2
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 + ope2 * (ope3 + ope4) - ope5
DISPLAY "23 + 26 * (2 + 8) - 50 = " WITH NO ADVANCING
COMPUTE NUM-03 = 23 + 26 * (2 + 8) - 50
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = -ope1 * ope2 + ope3 + (-(-ope4))
DISPLAY "-23 * 2.1 + 26.5 + (- (- 300.87)) = " WITH NO ADVANCING
COMPUTE NUM-03 = -23*2.1 + 26.5+(-(-300.87))
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 / ope2
DISPLAY "8 / 3 = " WITH NO ADVANCING
COMPUTE NUM-03 = 8/3.
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 / ope2
DISPLAY "8.12345 / 3.2 = " WITH NO ADVANCING
COMPUTE NUM-03 = 8.12345/3.2.
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 / ope2
DISPLAY "80.12345 / 3.12345 = " WITH NO ADVANCING
COMPUTE NUM-03 = 80.12345/3.12345.
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 ** ope2 / ope3
DISPLAY "4.2 ** 3 / 2.21 = " WITH NO ADVANCING
COMPUTE NUM-03 = 4.2**3 / 2.21
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 + ((ope2 * (ope3 / ope4 + ope5)) - ope6)
DISPLAY "23 + ((26 * (6 / 2 + 8)) - 50) = " WITH NO ADVANCING
COMPUTE NUM-03 = 23 + ((26 * (6 / 2 + 8)) - 50)
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 ** ope2 + ope3 ** ope4
DISPLAY "3 ** 2 + 2 ** 4 = " WITH NO ADVANCING
COMPUTE NUM-03 = 3 ** 2 + 2 ** 4
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

*   COMPUTE res = ope1 + ope2 ** ope3 + ope4 ** ope5
DISPLAY "3 + 26 ** 1 + 2 ** 3 = " WITH NO ADVANCING
COMPUTE NUM-03 = 3 + 26**1 + 2**3
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

MOVE 13.2     TO N1
MOVE 5.45     TO N2
MOVE 21       TO N3
MOVE 6234.2   TO N4
MOVE -6342.25 TO N5
MOVE 21       TO N6
MOVE 1.42     TO N7
MOVE 2        TO N8

DISPLAY N1 " * 2792.578 ** " N8 " = " WITH NO ADVANCING
COMPUTE NUM-03 = N1 * 2792.578 ** N8
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

DISPLAY N1 " * (" N2 " - (" N3 " * (" N4 " + " N5 ") + " N6 ") * "
N7 ") ** " N8 " = " WITH NO ADVANCING
COMPUTE NUM-03 = N1 * (N2 - (N3 * (N4 + N5) + N6) * N7) ** N8
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

DISPLAY "(" N3 " * (" N4 " + " N5 ") + " N6 ")"" = " WITH NO ADVANCING
COMPUTE NUM-03 = (N3 * (N4 + N5) + N6)
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

DISPLAY "(" 21 " * (" 6234.2 " + " -6342.25 ") + " 21 ")"" = " WITH NO ADVANCING
COMPUTE NUM-03 = (21 * (6234.2 -6342.25) + 21)
MOVE NUM-03 TO EDI-01 DISPLAY EDI-01

STOP RUN
.
* =================================================================

* The result is:

* 12345.678 =     12345.678
* 23.8765432 + 120026.01 =    120049.886
* 23 * 2 + 26 =        72.000
* 23 + 26 * 2 =        75.000
* 23 + 26 * (2 + 8) - 50 =       233.000
* -23 * 2.1 + 26.5 + (- (- 300.87)) =       279.070
* 8 / 3 =         2.666
* 8.12345 / 3.2 =         2.538
* 80.12345 / 3.12345 =        25.652
* 4.2 ** 3 / 2.21 =        33.523
* 23 + ((26 * (6 / 2 + 8)) - 50) =       259.000
* 3 ** 2 + 2 ** 4 =        25.000
* 3 + 26 ** 1 + 2 ** 3 =        37.000
* +00013200 * 2792.578 ** +00002 = 102940092.895
* +00013200 * (+0000545 - (+000210000 * (+062342000 + -0634225) + +00000021) * +00001420) ** +00002 = 134972161.869
* (+000210000 * (+062342000 + -0634225) + +00000021) =     -2248.050
* (21 * (6234.2 + -6342.25) + 21) =     -2248.050
```
Share:

Didn't find what you were looking for? Find more on Evaluates an arithmetic expression and stores the result Or get search suggestion and latest updates.

 Tyler Evans author of Evaluates an arithmetic expression and stores the result is from London, United Kingdom. View All Articles

 Please enter your CommentComment should be atleast 30 Characters.Please put code inside [Code] your code [/Code]. No Comment Found, Be the First to post comment!