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

Leap Year Check

Problem Description

Accept a year and determine if it is a leap year.

Expected Output

Input: 2024 -> Output: 2024 IS A LEAP YEAR

Hints

Leap year: divisible by 4, but not 100 unless also 400.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. LEAPYEAR. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-YEAR PIC 9(4). 01 WS-REM PIC 9(4). 01 WS-TEMP PIC 9(4). PROCEDURE DIVISION. DISPLAY "ENTER YEAR: ". ACCEPT WS-YEAR. DIVIDE WS-YEAR BY 400 GIVING WS-TEMP REMAINDER WS-REM. IF WS-REM = 0 DISPLAY WS-YEAR " IS A LEAP YEAR" ELSE DIVIDE WS-YEAR BY 100 GIVING WS-TEMP REMAINDER WS-REM IF WS-REM = 0 DISPLAY WS-YEAR " IS NOT A LEAP YEAR" ELSE DIVIDE WS-YEAR BY 4 GIVING WS-TEMP REMAINDER WS-REM IF WS-REM = 0 DISPLAY WS-YEAR " IS A LEAP YEAR" ELSE DISPLAY WS-YEAR " IS NOT A LEAP YEAR" END-IF END-IF END-IF. STOP RUN.

Explanation:

DIVIDE with REMAINDER gives modulo. Check 400 first, then 100, then 4.