Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Arithmetic Operations
Arithmetic Operations | Beginner | COBOL

Simple Addition

Problem Description

Write a program that accepts two numbers and displays their sum.

Expected Output

Enter 10 and 20, output: SUM = 30

Hints

Use ADD statement or COMPUTE. Define numeric variables with PIC 9.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. ADDITION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC 9(4). 01 WS-NUM2 PIC 9(4). 01 WS-RESULT PIC 9(5). PROCEDURE DIVISION. DISPLAY "ENTER FIRST NUMBER: ". ACCEPT WS-NUM1. DISPLAY "ENTER SECOND NUMBER: ". ACCEPT WS-NUM2. ADD WS-NUM1 TO WS-NUM2 GIVING WS-RESULT. DISPLAY "SUM = " WS-RESULT. STOP RUN.

Explanation:

ADD X TO Y GIVING Z adds X and Y, stores in Z.