Check if a given year is leap year in PL/SQL

PL/SQLMySQLDatabase

Here we will see how to check given year is leap year or not, using PL/SQL. In PL/SQL code, some group of commands are arranged within a block of related declaration of statements.

The leap year checking algorithm is like below.

Algorithm

isLeapYear(year):
begin
   if year is divisible by 4 and not divisible by 100, then
      it is leap year
   else if the number is divisible by 400, then
      it is leap year
   else
      it is not leap year
end

Example

DECLARE
   year NUMBER := 2012;
BEGIN
   IF MOD(year, 4)=0
      AND
      MOD(year, 100)!=0
      OR
      MOD(year, 400)=0 THEN
      dbms_output.Put_line(year || ' is leap year ');
   ELSE
      dbms_output.Put_line(year || ' is not leap year.');
   END IF;
END;

Output

2012 is leap year
raja
Published on 27-Sep-2019 11:15:44
Advertisements