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

Check Positive or Negative

Problem Description

Accept a number and determine if it is positive, negative, or zero.

Expected Output

Input: -5 -> Output: NUMBER IS NEGATIVE

Hints

Use IF-ELSE with comparison operators.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. POSNEG. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM PIC S9(5). PROCEDURE DIVISION. DISPLAY "ENTER A NUMBER: ". ACCEPT WS-NUM. IF WS-NUM > 0 DISPLAY "NUMBER IS POSITIVE" ELSE IF WS-NUM < 0 DISPLAY "NUMBER IS NEGATIVE" ELSE DISPLAY "NUMBER IS ZERO" END-IF. STOP RUN.

Explanation:

S in PIC allows signed numbers. Nested IF-ELSE handles multiple conditions.