使用 PL/SQL 檢查給定年份是否是閏年


我們將在本文中學習如何使用 PL/SQL 檢查給定的年份是否是閏年。在 PL/SQL 程式碼中,一些命令組按相關宣告塊進行排列。

閏年檢查演算法如下。

演算法

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

示例

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;

輸出

2012 is leap year

更新於: 2019 年 9 月 27 日

3 千次瀏覽

開啟您的職業生涯

完成課程並獲得認證

開始學習
廣告