Subtract Verb in COBOL

Subtract Verb

Subtract verb is used to subtract one numeric item or the sum of two or more numeric items from one or more numeric items and store the result.

Syntax

SUBTRACT A B FROM C D                             ---------- Syntax 1

SUBTRACT A B C FROM D GIVING E      ---------- Syntax 2

SUBTRACT  CORR WS-GR1 TO WS-GR2        ---------- Syntax 3


In syntax 1, A & B are added and subtracted from C and the result is stored in C. i.e C=(C-(A+B)). A & B are added and subtracted from D. The result is stored in D.
i.e D= (D- (A+B)).

In syntax 2, A, B & C are added and subtracted from D. Result is stored in E. (E=D-(A+B+C)).

In syntax 3, sub-group item in WS-GR1 and WS-GR2 are subtracted and the result is stored in WS-GR2.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. SV.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-N1 PIC 9(5) VALUE 25.
   01 WS-N2 PIC 9(3) VALUE 15.
   01 WS-N3 PIC 9(4) VALUE 110.
   01 WS-N4 PIC 9(3) VALUE 80.
   01 WS-NA PIC 9(2) VALUE 12.
   01 WS-NB PIC 9(2) VALUE 8.
   01 WS-NC PIC 9(3) VALUE 10.
   01 WS-ND PIC 9(4) VALUE 90.
   01 WS-NE PIC 9(3) VALUE 10.

PROCEDURE DIVISION.
   SUBTRACT WS-N1 WS-N2 FROM WS-N3 WS-N4.
   SUBTRACT WS-NA WS-NB WS-NC FROM WS-ND GIVING WS-NE.

   DISPLAY "WS-NUMBER-1     : " WS-N1
   DISPLAY "WS-NUMBER-2     : " WS-N2
   DISPLAY "WS-NUMBER-3     : " WS-N3
   DISPLAY "WS-NUMBER-4     : " WS-N4
   DISPLAY "WS-NUMBER-A     : " WS-NA
   DISPLAY "WS-NUMBER-B     : " WS-NB
   DISPLAY "WS-NUMBER-C     : " WS-NC
   DISPLAY "WS-NUMBER-D     : " WS-ND
   DISPLAY "WS-NUMBER-E     : " WS-NE

STOP RUN.


Output:
WS-NUMBER-1     : 00025
WS-NUMBER-2     : 015
WS-NUMBER-3     : 0070
WS-NUMBER-4     : 040
WS-NUMBER-A     : 12
WS-NUMBER-B     : 08
WS-NUMBER-C     : 010
WS-NUMBER-D     : 0090
WS-NUMBER-E     : 060