Skip to content
Related Articles

Related Articles

Check if a given year is leap year in PL/SQL
  • Difficulty Level : Basic
  • Last Updated : 02 Jul, 2018

Prerequisite PL/SQL introduction

In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.

Given a year, the task is to check that given year is a leap year or not.

Examples:

Input: 1500
Output: 1500 is not leap year.

Input: 1600
Output: 1600 is a leap year

A year is a leap year if following conditions are satisfied:
1) Year is multiple of 400
2) Year is multiple of 4 and not multiple of 100




-- To check if a
-- given year is leap year or not
DECLARE
  year NUMBER := 1600;
BEGIN
  --  true if the year is a multiple
  -- of 4 and not multiple of 100.
  -- OR year is multiple of 400.
  IF MOD(year, 4)=0
    AND
    MOD(year, 100)!=0
    OR
    MOD(year, 400)=0 THEN
    dbms_output.Put_line(year
    || ' is a leap year ');
  ELSE
    dbms_output.Put_line(year
    || ' is not a leap year.');
  END IF;
END

Output:

1600 is a leap year.
My Personal Notes arrow_drop_up
Recommended Articles
Page :