Leap year: Difference between revisions

29,633 bytes added ,  2 months ago
(A)
 
(79 intermediate revisions by 38 users not shown)
Line 12:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F is_leap_year(year)
I year % 100 == 0
R year % 400 == 0
R year % 4 == 0</langsyntaxhighlight>
 
=={{header|360 Assembly}}==
Line 27:
When R15 = zero, the year is a leap year.
Otherwise it is not.
<langsyntaxhighlight lang="360 Assemblyassembly">
LPCK CSECT
USING LPCK,15
Line 46:
BR 14
END
</langsyntaxhighlight>
 
Sample invocation from a COBOL program:
Line 72:
=={{header|68000 Assembly}}==
 
<langsyntaxhighlight lang="68000 Assemblyassembly">;Example
move.l #2018,d0
bsr leap_year
Line 116:
moveq.l #0,d1
rts
</syntaxhighlight>
</lang>
 
=={{header|8080 Assembly}}==
 
<langsyntaxhighlight lang="8080asm"> org 100h
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 180:
jmp 5
no: db 'NOT '
yes: db 'LEAP YEAR.$'</langsyntaxhighlight>
 
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC IsLeapYear(CARD year)
IF year MOD 100=0 THEN
IF year MOD 400=0 THEN
RETURN (1)
ELSE
RETURN (0)
FI
FI
IF year MOD 4=0 THEN
RETURN (1)
FI
RETURN (0)
 
PROC Main()
CARD ARRAY t=[1900 1901 2000 2001 2004 2020 2021]
BYTE i,leap
CARD year
 
FOR i=0 TO 6
DO
year=t(i)
leap=IsLeapYear(year)
IF leap=0 THEN
PrintF("%U is not a leap year%E",year)
ELSE
PrintF("%U is a leap year%E",year)
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Leap_year.png Screenshot from Atari 8-bit computer]
<pre>
1900 is not a leap year
1901 is not a leap year
2000 is a leap year
2001 is not a leap year
2004 is a leap year
2020 is a leap year
2021 is not a leap year
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">public function isLeapYear(year:int):Boolean {
if (year % 100 == 0) {
return (year % 400 == 0);
}
return (year % 4 == 0);
}</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">-- Incomplete code, just a sniplet to do the task. Can be used in any package or method.
-- Adjust the type of Year if you use a different one.
function Is_Leap_Year (Year : Integer) return Boolean is
Line 226 ⟶ 269:
 
-- To improve speed a bit more, use with
pragma Inline (Is_Leap_Year);</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|A60}}
<langsyntaxhighlight lang="algol60">begin
integer year;
 
Line 243 ⟶ 286:
if isLeapYear(year) then outstring(1,"True\n") else outstring(1, "False\n")
end for year
end </langsyntaxhighlight>
{{out}}
<pre>
Line 265 ⟶ 308:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format''ted transput}}
<langsyntaxhighlight lang="algol68">MODE YEAR = INT, MONTH = INT, DAY = INT;
PROC year days = (YEAR year)DAY: # Ignore 1752 CE for the moment #
Line 283 ⟶ 326:
printf(($g(0)" is "b("","not ")"a leap year."l$, year, is leap year(year)))
OD
)</langsyntaxhighlight>
{{out}}
<pre>
Line 294 ⟶ 337:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% returns true if year is a leap year, false otherwise %
% assumes year is in the Gregorian Calendar %
Line 309 ⟶ 352:
)
end for_year
end.</langsyntaxhighlight>
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algol">
BEGIN
 
Line 356 ⟶ 399:
 
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 380 ⟶ 423:
 
=={{header|APL}}==
Returns 1 if leap year, 0 otherwise:
<lang apl>
<syntaxhighlight lang="apl">
⍝returns 1 if leapyear, 0 otherwise:
∇ z←Leap year
∇Z←LEAPYEAR YEAR
Z←(0=4|YEARyear)∧(0=400|YEARyear)∨~0=100|YEARyear
</syntaxhighlight>
</lang>
A much neater version of the above relies on the fact that every rule is an exception the the previous one:
<syntaxhighlight lang="apl">
∇ z←Leap year
z←0≠.=400 100 4∘.|year
</syntaxhighlight>
This essentially works by running an XOR reduction over the divisibility by 4, 100, and 400. Some APL implementations support tacit (a.k.a. points-free) programming:
<syntaxhighlight lang="apl">
Leap←0≠.=400 100 4∘.|⊢
</syntaxhighlight>
Dyalog APL version 18.0 added a built-in date-time function:
<syntaxhighlight lang="apl">
Leap←0⎕DT,∘2 29¨
</syntaxhighlight>
This works by extending the year to February 29 of that year, and then checking if the date is valid.
 
With any of the above definitions, no loop is necessary to check each year of an array:
<syntaxhighlight lang="apl">
Leap 1899 1900 1901 1902 1903 1904 1905 1999 2000 2001 2002 2003 2004
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="apl">
0 0 0 0 0 1 0 0 1 0 0 0 1
</syntaxhighlight>
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on leap_year(y)
return y mod 4 is equal to 0 and (y mod 100 is not equal to 0 or y mod 400 is equal to 0)
end leap_year
 
leap_year(1900)</langsyntaxhighlight>
 
=={{header|Arc}}==
<langsyntaxhighlight lang="arc">
(= leap? (fn (year)
(if (and (is 0 (mod year 4)) (isnt 0 (mod year 100))) year
(unless (< 0 (+ (mod year 100) (mod year 400))) year))))
</syntaxhighlight>
</lang>
<b>Output:</b>
<langsyntaxhighlight lang="arc">
(map [leap? _] '(1900 1904 2000 2019 2020 2100))
;; => '( 1904 2000 2020 )
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">years: [
1600 1660 1724 1788 1848 1912 1972
2032 2092 2156 2220 2280 2344 2348
1698 1699 1700 1750 1800 1810 1900
1901 1973 2100 2107 2200 2203 2289
]
 
print select years => leap?</syntaxhighlight>
 
{{out}}
 
<pre>1600 1660 1724 1788 1848 1912 1972 2032 2092 2156 2220 2280 2344 2348</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">leapyear(year)
{
if (Mod(year, 100) = 0)
Line 414 ⟶ 495:
}
 
MsgBox, % leapyear(1604)</langsyntaxhighlight>
{{out}}
<pre>Returns 1 if year is a leap year</pre>
or
<langsyntaxhighlight lang="autohotkey">IsLeapYear(Year)
{
return !Mod(Year, 4) && Mod(Year, 100) || !Mod(Year, 400)
}
 
MsgBox % "The year 1604 was " (IsLeapYear(1604) ? "" : "not ") "a leap year"</langsyntaxhighlight>
{{out}}
<pre>The year 1600 was a leap year
Line 430 ⟶ 511:
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">; AutoIt Version: 3.3.8.1
$Year = 2012
$sNot = " not"
Line 442 ⟶ 523:
 
; == But it exists the standard UDF "Date.au3" with this function: "_IsLeapYear($Year)"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 450 ⟶ 531:
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">function leapyear( year )
{
if ( year % 100 == 0 )
Line 456 ⟶ 537:
else
return ( year % 4 == 0 )
}</langsyntaxhighlight>
 
=={{header|Bash}}==
<syntaxhighlight lang="bash">
<lang Bash>
#!/bin/bash
 
Line 499 ⟶ 580:
# Save the above to a file named is_leap_year.sh, then issue the following command to run the 5 tests of the function
# bash is_leap_year.sh
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|QBasic}}
A one-liner combination from the [[#Commodore_BASIC|Commodore BASIC]] and [[#GW_BASIC|GW-BASIC]] solutions.
<syntaxhighlight lang="gwbasic">FOR Y = 1750 TO 2021: PRINT MID$ ( STR$ (Y) + " ",1,5 * (Y / 4 = INT (Y / 4)) * ((Y / 100 < > INT (Y / 100)) + (Y / 400 = INT (Y / 400))));: NEXT</syntaxhighlight>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256"># year is a BASIC-256 keyword
function leapyear(year_)
if (year_ mod 4) <> 0 then return FALSE
if (year_ mod 100) = 0 and (year_ mod 400) <> 0 then return FALSE
return TRUE
end function
 
for year_ = 1800 to 2900 step 100
print year_;
if leapyear(year_) then print " is a leap year" else print " is not a leap year"
next year_
 
print
 
for year_ = 2012 to 2031
print year_;
if leapyear(year_) = TRUE then print " = leap "; else print " = no ";
if (year_ mod 4) = 3 then print ""
next year_
end</syntaxhighlight>
 
==={{header|BaCon}}===
From the Ada shortcut calculation
<syntaxhighlight lang="qbasic">' Leap year
FUNCTION leapyear(NUMBER y) TYPE NUMBER
RETURN IIF(MOD(y, 4) = 0, IIF(MOD(y, 16) = 0, IIF(MOD(y, 100) != 0, TRUE, FALSE), TRUE), FALSE)
END FUNCTION
 
READ y
WHILE y != 0
PRINT y, ": ", IIF$(leapyear(y), "", "not a "), "leapyear"
READ y
WEND
 
DATA 1600, 1700, 1800, 1900, 1901, 1996, 2000, 2001, 2004, 0</syntaxhighlight>
 
{{out}}
<pre>1600: not a leapyear
1700: leapyear
1800: leapyear
1900: leapyear
1901: not a leapyear
1996: leapyear
2000: not a leapyear
2001: not a leapyear
2004: leapyear
</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> REPEAT
INPUT "Enter a year: " year%
IF FNleap(year%) THEN
PRINT ;year% " is a leap year"
ELSE
PRINT ;year% " is not a leap year"
ENDIF
UNTIL FALSE
END
DEF FNleap(yr%)
= (yr% MOD 4 = 0) AND ((yr% MOD 400 = 0) OR (yr% MOD 100 <> 0))</syntaxhighlight>
 
Much quicker without full evaluation:
 
<syntaxhighlight lang="bbcbasic">DEFFNleap(yr%)
IF yr% MOD 4 THEN =FALSE
IF yr% MOD 400 ELSE =TRUE
IF yr% MOD 100 ELSE =FALSE
=TRUE</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">
10 rem Leap year
20 for i% = 1 to 5
30 read year%
40 print year%;"is ";
50 if isleapyear(year%) = 0 then print "not "; else print "";
60 print "a leap year."
70 next i%
80 end
 
200 data 1900,1994,1996,1997,2000
 
400 sub isleapyear(y%)
410 isleapyear = ((y% mod 4 = 0) and (y% mod 100 <> 0)) or (y% mod 400 = 0)
420 end sub
</syntaxhighlight>
{{out}}
<pre>
1900 is not a leap year.
1994 is not a leap year.
1996 is a leap year.
1997 is not a leap year.
2000 is a leap year.
</pre>
 
==={{header|Commodore BASIC}}===
An old-timey solution:
<syntaxhighlight lang="basic">10 DEF FNLY(Y)=(Y/4=INT(Y/4))*((Y/100<>INT(Y/100))+(Y/400=INT(Y/400)))</syntaxhighlight>
 
Or, using Simons' BASIC's MOD function:
===={{header|Simons' BASIC}}====
<syntaxhighlight lang="basic">10 DEF FNLY(Y)=(0=MOD(Y,4))*((0<MOD(Y,100))+(0=MOD(Y,400)))</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 23-06-2015
' compile with: fbc -s console
 
#Ifndef TRUE ' define true and false for older freebasic versions
#Define FALSE 0
#Define TRUE Not FALSE
#EndIf
 
Function leapyear(Year_ As Integer) As Integer
 
If (Year_ Mod 4) <> 0 Then Return FALSE
If (Year_ Mod 100) = 0 AndAlso (Year_ Mod 400) <> 0 Then Return FALSE
Return TRUE
 
End Function
 
' ------=< MAIN >=------
 
' year is a FreeBASIC keyword
Dim As Integer Year_
 
For Year_ = 1800 To 2900 Step 100
Print Year_; IIf(leapyear(Year_), " is a leap year", " is not a leap year")
Next
 
Print : Print
 
For Year_ = 2012 To 2031
Print Year_;
If leapyear(Year_) = TRUE Then
Print " = leap",
Else
Print " = no",
End If
If year_ Mod 4 = 3 Then Print ' lf/cr
Next
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre> 1800 is not a leap year
1900 is not a leap year
2000 is a leap year
2100 is not a leap year
2200 is not a leap year
2300 is not a leap year
2400 is a leap year
2500 is not a leap year
2600 is not a leap year
2700 is not a leap year
2800 is a leap year
2900 is not a leap year
 
2012 = leap 2013 = no 2014 = no 2015 = no
2016 = leap 2017 = no 2018 = no 2019 = no
2020 = leap 2021 = no 2022 = no 2023 = no
2024 = leap 2025 = no 2026 = no 2027 = no
2028 = leap 2029 = no 2030 = no 2031 = no</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1
 
// In-line C function to generate random number in range
BeginCFunction
long randomInRange( long min, long max ) {
int i = (arc4random()%(max-min+1))+min;
return (long)i;
}
EndC
toolbox fn randomInRange( long min, long max ) = long
 
// Leap year test function
local fn LeapYear( year as long ) as BOOL
BOOL result : result = _false
if year mod 400 == 0 then result = _true : exit fn
if year mod 100 == 0 then result = _false : exit fn
if year mod 4 == 0 then result = _true : exit fn
if year mod 4 != 0 then result = _false : exit fn
end fn = result
 
long i, y, knownLeapYear(10)
 
// Array of known leap years from 1980 through 2020 for control
knownLeapYear(0) = 1980 : knownLeapYear(1) = 1984 : knownLeapYear(2) = 1988
knownLeapYear(3) = 1992 : knownLeapYear(4) = 1996 : knownLeapYear(5) = 2000
knownLeapYear(6) = 2004 : knownLeapYear(7) = 2008 : knownLeapYear(8) = 2012
knownLeapYear(9) = 2016 : knownLeapYear(10) = 2020
 
print "Known leap years:"
for i = 0 to 9
if ( fn LeapYear( knownLeapYear(i) ) == _true )
print knownLeapYear(i); " is a leap year."
else
print knownLeapYear(i); " is a not leap year."
end if
next
 
print
 
// Random years from 1980 to 2020 to test
print "Check random years:"
for i = 0 to 20
y = fn randomInRange( 1980, 2020 )
if ( fn LeapYear( y ) == _true )
print y; " is a leap year."
else
print y; " is a not leap year."
end if
next
 
HandleEvents</syntaxhighlight>
 
Output (results will vary for random years):
<pre>
Known leap years:
1980 is a leap year.
1984 is a leap year.
1988 is a leap year.
1992 is a leap year.
1996 is a leap year.
2000 is a leap year.
2004 is a leap year.
2008 is a leap year.
2012 is a leap year.
2016 is a leap year.
 
Check random years:
1998 is a not leap year.
1987 is a not leap year.
2015 is a not leap year.
1998 is a not leap year.
2020 is a leap year.
2020 is a leap year.
2009 is a not leap year.
2020 is a leap year.
2018 is a not leap year.
2013 is a not leap year.
2003 is a not leap year.
1994 is a not leap year.
1989 is a not leap year.
1999 is a not leap year.
1984 is a leap year.
1980 is a leap year.
1998 is a not leap year.
2008 is a leap year.
1983 is a not leap year.
2007 is a not leap year.
2004 is a leap year.
</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim dDate As Date
Dim siYear As Short = InputBox("Enter a year", "Leap year test")
Dim sMessage As String = " is a leap year."
 
Try dDate = Date(siYear, 02, 29)
If Error Then sMessage = " is not a leap year."
 
Message(siYear & sMessage)
 
End</syntaxhighlight>
 
Output:
<pre>
2016 is a leap year.
</pre>
 
==={{header|GW-BASIC}}===
==== With a function ====
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">
10 ' Leap year
20 DEF FN ISLEAPYEAR(Y%) = ((Y% MOD 4 = 0) AND (Y% MOD 100 <> 0)) OR (Y% MOD 400 = 0)
95 ' *** Test ***
100 FOR I% = 1 TO 5
110 READ YEAR%
120 PRINT YEAR%; "is ";
130 IF FN ISLEAPYEAR(YEAR%) = 0 THEN PRINT "not "; ELSE PRINT "";
140 PRINT "a leap year."
150 NEXT I%
160 END
200 DATA 1900, 1994, 1996, 1997, 2000
</syntaxhighlight>
{{out}}
<pre>
1900 is not a leap year.
1994 is not a leap year.
1996 is a leap year.
1997 is not a leap year.
2000 is a leap year.
</pre>
 
==== With a subroutine ====
Prints all the leap years from 1750 to 2021. Note the correct behaviour of 1800, 1900, and 2000.
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">10 FOR Y = 1750 TO 2021
20 GOSUB 1000
30 IF L = 1 THEN PRINT Y;" ";
40 NEXT Y
50 END
1000 L = 0
1010 IF Y MOD 4 <> 0 THEN RETURN
1020 IF Y MOD 100 = 0 AND Y MOD 400 <> 0 THEN RETURN
1030 L = 1
1040 RETURN</syntaxhighlight>
{{out}}<pre>
1752 1756 1760 1764 1768 1772 1776 1780 1784 1788 1792 1796 1804 1808 1812 1816 1820 1824 1828
1832 1836 1840 1844 1848 1852 1856 1860 1864 1868 1872 1876 1880 1884 1888 1892 1896 1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980
1984 1988 1992 1996 2000 2004 2008 2012 2016 2020</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Leapyear.bas"
110 FOR I=1990 TO 2020
120 IF LEAPY(I) THEN
130 PRINT I;"is a leap year."
140 ELSE
150 PRINT I;"is not a leap year."
160 END IF
170 NEXT
180 DEF LEAPY(Y)=MOD(Y,4)=0 AND MOD(Y,100) OR MOD(Y,400)=0</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
==== Simple method ====
<syntaxhighlight lang="lb">if leap(1996)then
print "leap"
else
print "ordinary"
end if
wait
 
function leap(n)
leap=date$("2/29/";n)
end function</syntaxhighlight>
 
==== Calculated method ====
<syntaxhighlight lang="lb"> year = 1908
select case
case year mod 400 = 0
leapYear = 1
case year mod 4 = 0 and year mod 100 <> 0
leapYear = 1
case else
leapYear = 0
end select
if leapYear = 1 then
print year;" is a leap year."
else
print year;" is not a leap year."
end if</syntaxhighlight>
 
==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">10 INPUT "ENTER A NUMBER, AND I'LL DETECT IF IT'S A LEAP YEAR OR NOT. ",A
20 IF A-(A/100)*100=0 AND A-(A/400)*400<>0 THEN RESULT$="NOT "
30 PRINT "THAT'S "RESULT$"A LEAP YEAR."</syntaxhighlight>
 
==={{header|Palo Alto Tiny BASIC}}===
{{trans|Tiny BASIC}}
<syntaxhighlight lang="basic">
10 REM LEAP YEAR
20 FOR Y=1750 TO 2022
30 GOSUB 100
40 IF L=1 PRINT Y
50 NEXT Y
60 STOP
100 LET L=0
110 IF Y-(Y/4)*4#0 RETURN
120 IF Y-(Y/100)*100#0 LET L=1
130 IF Y-(Y/400)*400=0 LET L=1
140 RETURN</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure isLeapYear(Year)
If (Year%4=0 And Year%100) Or Year%400=0
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure</syntaxhighlight>
 
==={{header|QBasic}}===
Note that the <code>year%</code> function is not needed for most modern BASICs.
<langsyntaxhighlight lang="qbasic">DECLARE FUNCTION diy% (y AS INTEGER)
DECLARE FUNCTION isLeapYear% (yr AS INTEGER)
DECLARE FUNCTION year% (date AS STRING)
Line 528 ⟶ 1,005:
FUNCTION year% (date AS STRING)
year% = VAL(RIGHT$(date, 4))
END FUNCTION</langsyntaxhighlight>
 
An old-timey solution:
 
{{works with|Commodore BASIC}}
<lang BASIC>10 DEF FNLY(Y)=(Y/4=INT(Y/4))*((Y/100<>INT(Y/100))+(Y/400=INT(Y/400)))</lang>
 
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 PROGRAM "Leapyear.bas"
110 FOR I=1990 TO 2020
120 IF LEAPY(I) THEN
130 PRINT I;"is a leap year."
140 ELSE
150 PRINT I;"is not a leap year."
160 END IF
170 NEXT
180 DEF LEAPY(Y)=MOD(Y,4)=0 AND MOD(Y,100) OR MOD(Y,400)=0</lang>
 
==={{header|Sinclair ZX81 BASIC}}===
ZX81 BASIC does not support user-defined functions, even the single-expression functions that are provided by many contemporary dialects; so we have to fake it using a subroutine and pass everything in global variables.
<lang basic>5000 LET L=Y/4=INT (Y/4) AND (Y/100<>INT (Y/100) OR Y/400=INT (Y/400))
5010 RETURN</lang>
An example showing how to call it:
<lang basic>10 INPUT Y
20 GOSUB 5000
30 PRINT Y;" IS ";
40 IF NOT L THEN PRINT "NOT ";
50 PRINT "A LEAP YEAR."
60 STOP</lang>
 
==={{header|ZX Spectrum Basic}}===
<lang zxbasic>10 DEF FN l(y)=y/4=INT (y/4) AND (y/100<>INT (y/100) OR y/400=INT (y/400))
</lang>
 
==={{header|QL SuperBASIC}}===
<langsyntaxhighlight lang="qbasic">
AUTO 100,10
REM Is% a non-proleptic Gregorian year y$<=9999 leap (0) 0R ordinary (1)?
DEF FN Is%(y$)
LOC l%,c%,y%
LET c%=y$(1 TO 2)&"00" : l%=c% MOD 16 AND cy%=y$ OR y$ MOD 4
LET l%=c% MOD 16 AND y$(3 TO 4)="00" OR y% MOD 4
RETurn l%
RET l%
END DEF Is%
ctrl+space
</syntaxhighlight>
</lang>
using only power-of-2 divisions. N.B. the inverted logic brings home the BaCon code's flaw
{{output}}<pre>
<pre>
1600 0
1700 1
Line 583 ⟶ 1,030:
</pre>
 
==={{header|BaConRun BASIC}}===
<syntaxhighlight lang="runbasic">if date$("02/29/" + mid$(date$("mm/dd/yyyy"),7,4)) then print "leap year" else print "not"</syntaxhighlight>
From the Ada shortcut calculation
<lang qbasic>' Leap year
FUNCTION leapyear(NUMBER y) TYPE NUMBER
RETURN IIF(MOD(y, 4) = 0, IIF(MOD(y, 16) = 0, IIF(MOD(y, 100) != 0, TRUE, FALSE), TRUE), FALSE)
END FUNCTION
 
==={{header|S-BASIC}}===
READ y
Since S-BASIC has no MOD operator or function, we have to supply one.
WHILE y != 0
<syntaxhighlight lang="basic">
PRINT y, ": ", IIF$(leapyear(y), "", "not a "), "leapyear"
rem - compute p mod q
READ y
function mod(p, q = integer) = integer
WEND
end = p - q * (p/q)
 
rem - return true (-1) if y is a leap year, otherwise 0
DATA 1600, 1700, 1800, 1900, 1901, 1996, 2000, 2001, 2004, 0</lang>
function isleapyear(y = integer) = integer
end = mod(y,4)=0 and mod(y,100)<>0 or mod(y,400)=0
 
rem - exercise the function
var y = integer
 
print "Test of century years"
for y = 1600 to 2000 step 100
if isleapyear(y) then
print y;" is a leap year"
else
print y;" is NOT a leap year"
next y
 
print "Test of current half-decade"
for y = 2015 to 2020
if isleapyear(y) then
print y; " is a leap year"
else
print y; " is NOT a leap year"
next y
 
end
</syntaxhighlight>
{{out}}
<pre>
<pre>1600: not a leapyear
Test of century years
1700: leapyear
1600 is a leap year
1800: leapyear
1700 is NOT a leap year
1900: leapyear
1800 is NOT a leap year
1901: not a leapyear
1900 is NOT a leap year
1996: leapyear
2000: notis a leapyearleap year
Test of current half-decade
2001: not a leapyear
2015 is NOT a leap year
2004: leapyear
2016 is a leap year
2017 is NOT a leap year
2018 is NOT a leap year
2019 is NOT a leap year
2020 is a leap year
</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
ZX81 BASIC does not support user-defined functions, even the single-expression functions that are provided by many contemporary dialects; so we have to fake it using a subroutine and pass everything in global variables.
<syntaxhighlight lang="basic">5000 LET L=Y/4=INT (Y/4) AND (Y/100<>INT (Y/100) OR Y/400=INT (Y/400))
5010 RETURN</syntaxhighlight>
An example showing how to call it:
<syntaxhighlight lang="basic">10 INPUT Y
20 GOSUB 5000
30 PRINT Y;" IS ";
40 IF NOT L THEN PRINT "NOT ";
50 PRINT "A LEAP YEAR."
60 STOP</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">REM Rosetta Code problem: https://rosettacode.org/wiki/Leap_year
REM by Jjuanhdez, 06/2022
 
10 REM Leap year
20 LET Y = 1750
30 IF Y = 2021 THEN GOTO 80
40 GOSUB 100
50 IF L = 1 THEN PRINT Y
60 LET Y = Y + 1
70 GOTO 30
80 END
100 LET L = 0
110 IF (Y - (Y / 4) * 4) <> 0 THEN RETURN
120 IF (Y - (Y / 100) * 100) = 0 THEN GOTO 140
130 LET L = 1
140 IF (Y - (Y / 400) * 400) <> 0 THEN GOTO 160
150 LET L = 1
160 RETURN</syntaxhighlight>
 
==={{header|uBasic/4tH}}===
{{trans|BBC BASIC}}
<syntaxhighlight lang="text">DO
INPUT "Enter a year: "; y
IF FUNC(_FNleap(y)) THEN
PRINT y; " is a leap year"
ELSE
PRINT y; " is not a leap year"
ENDIF
LOOP
END
 
_FNleap Param (1)
RETURN ((a@ % 4 = 0) * ((a@ % 400 = 0) + (a@ % 100 # 0)))</syntaxhighlight>
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">Public Function Leap_year(year As Integer) As Boolean
Leap_year = (Month(DateSerial(year, 2, 29)) = 2)
End Function</syntaxhighlight>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
Function IsLeapYear(yr)
IsLeapYear = False
If yr Mod 4 = 0 And (yr Mod 400 = 0 Or yr Mod 100 <> 0) Then
IsLeapYear = True
End If
End Function
 
'Testing the function.
arr_yr = Array(1900,1972,1997,2000,2001,2004)
 
For Each yr In arr_yr
If IsLeapYear(yr) Then
WScript.StdOut.WriteLine yr & " is leap year."
Else
WScript.StdOut.WriteLine yr & " is NOT leap year."
End If
Next
</syntaxhighlight>
 
{{Out}}
<pre>
1900 is NOT leap year.
1972 is leap year.
1997 is NOT leap year.
2000 is leap year.
2001 is NOT leap year.
2004 is leap year.
</pre>
 
==={{header|Visual Basic}}===
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
Public Function IsLeapYear1(ByVal theYear As Integer) As Boolean
'this function utilizes documented behaviour of the built-in DateSerial function
IsLeapYear1 = (VBA.Day(VBA.DateSerial(theYear, 2, 29)) = 29)
End Function
 
Public Function IsLeapYear2(ByVal theYear As Integer) As Boolean
'this function uses the well-known formula
IsLeapYear2 = IIf(theYear Mod 100 = 0, theYear Mod 400 = 0, theYear Mod 4 = 0)
End Function
</syntaxhighlight>
Testing:
<syntaxhighlight lang="vb">
Sub Main()
'testing the above functions
Dim i As Integer
For i = 1750 To 2150
Debug.Assert IsLeapYear1(i) Eqv IsLeapYear2(i)
Next i
End Sub
</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
For Each y In {1900, 1994, 1996, Date.Now.Year}
Console.WriteLine("{0} is {1}a leap year.", y, If(Date.IsLeapYear(y), String.Empty, "not "))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>1900 is not a leap year.
1994 is not a leap year.
1996 is a leap year.
2019 is not a leap year.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub leapyear(year)
if mod(year, 4) <> 0 then return false : fi
if mod(year, 100) = 0 and mod(year, 400) <> 0 then return false : fi
return TRUE
end sub
 
for year = 1800 to 2900 step 100
print year;
if leapyear(year) then print " is a leap year" else print " is not a leap year" : fi
next year
print
for year = 2012 to 2031
print year;
if leapyear(year) = TRUE then print " = leap "; else print " = no "; : fi
if mod(year, 4) = 3 then print : fi
next year
end</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="zxbasic">10 DEF FN l(y)=y/4=INT (y/4) AND (y/100<>INT (y/100) OR y/400=INT (y/400))
</syntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
 
::The Main Thing...
Line 639 ⟶ 1,259:
echo %year% is NOT a leap year.
goto :EOF
::/The Function...</langsyntaxhighlight>
{{out}}
<pre>1900 is NOT a leap year.
Line 657 ⟶ 1,277:
Press any key to continue . . .</pre>
 
 
=={{header|BBC BASIC}}==
=={{header|bc}}==
<lang bbcbasic> REPEAT
<syntaxhighlight lang="bc">define l(y) {
INPUT "Enter a year: " year%
if (y % 100 IF== FNleap(year%0) THENy /= 100
if (y PRINT ;year% " is4 a== leap0) year"return(1)
ELSEreturn(0)
}</syntaxhighlight>
PRINT ;year% " is not a leap year"
 
ENDIF
=={{header|BCPL}}==
UNTIL FALSE
<syntaxhighlight lang="bcpl">get "libhdr"
END
 
let leap(year) = year rem 400 = 0 | (year rem 4 = 0 & year rem 100 ~= 0)
DEF FNleap(yr%)
 
= (yr% MOD 4 = 0) AND ((yr% MOD 400 = 0) OR (yr% MOD 100 <> 0))</lang>
let start() be
$( let years = table 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1999,
2000, 2001, 2002, 2003, 2004, 2021, 2022
for i = 0 to 14 do
writef("%N %S a leap year.*N",
years!i, leap(years!i) -> "is", "is not")
$)</syntaxhighlight>
{{out}}
<pre>1899 is not a leap year.
1900 is not a leap year.
1901 is not a leap year.
1902 is not a leap year.
1903 is not a leap year.
1904 is a leap year.
1905 is not a leap year.
1999 is not a leap year.
2000 is a leap year.
2001 is not a leap year.
2002 is not a leap year.
2003 is not a leap year.
2004 is a leap year.
2021 is not a leap year.
2022 is not a leap year.</pre>
 
=={{header|Befunge}}==
{{trans|C}}
 
<langsyntaxhighlight lang="befunge">0"2("*:3-:1-:2-:"^"-v<
v*%"d"\!%4::,,"is".:<|
>\45*:*%!+#v_ "ton"vv<
v"ear."+550<,,,,*84<$#
>"y pael a ">:#,_$:#@^</langsyntaxhighlight>
 
{{out}}
Line 686 ⟶ 1,329:
1997 is not a leap year.
2000 is a leap year.</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Leap ← 0=4|100÷˜⍟(0=|)¨⊢</syntaxhighlight>
Or:
<syntaxhighlight lang="bqn">Leap ← -˝0=4‿100‿400|⌜⊢</syntaxhighlight>
Test:
<syntaxhighlight lang="bqn">Leap 1900‿1996‿1998‿1999‿2000‿2024‿2100</syntaxhighlight>
{{out}}
<pre>⟨ 0 1 0 0 1 1 0 ⟩</pre>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat"> ( leap-year
=
. mod$(!arg.100):0
Line 702 ⟶ 1,354:
)
)
& ;</langsyntaxhighlight>
{{out}}
<pre>1600 is a leap year
Line 713 ⟶ 1,365:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int is_leap_year(intunsigned year)
{
return (!(year % 4) && (year % 100 ||? !(year3 %: 40015)) ? 1 : 0;
}
 
int main(void)
{
intconst unsigned test_case[] = {1900, 1994, 1996, 1997, 2000}, key, end, year;
1900, 1994, 1996, 1997, 2000, 2024, 2025, 2026, 2100
for (key = 0, end = sizeof(test_case)/sizeof(test_case[0]); key < end; ++key) {
};
year = test_case[key];
const unsigned n = sizeof test_case / sizeof test_case[0];
printf("%d is %sa leap year.\n", year, (is_leap_year(year) == 1 ? "" : "not "));
 
for (unsigned i = 0; i != n; ++i) {
unsigned year = test_case[i];
printf("%u is %sa leap year.\n", year, is_leap_year(year) ? "" : "not ");
}
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
<pre>
Line 735 ⟶ 1,392:
1997 is not a leap year.
2000 is a leap year.
2024 is a leap year.
2025 is not a leap year.
2026 is not a leap year.
2100 is not a leap year.
</pre>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program
Line 751 ⟶ 1,412:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1900 is not a leap year.
Line 761 ⟶ 1,422:
Uses C++11. Compile with
g++ -std=c++11 leap_year.cpp
<langsyntaxhighlight lang="cpp">#include <iostream>
 
bool is_leap_year(int year) {
Line 771 ⟶ 1,432:
std::cout << year << (is_leap_year(year) ? " is" : " is not") << " a leap year.\n";
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 782 ⟶ 1,443:
 
=={{header|Clipper}}==
<langsyntaxhighlight Clipperlang="clipper">Function IsLeapYear( nYear )
Return Iif( nYear%100 == 0, (nYear%400 == 0), (nYear%4 == 0) )</langsyntaxhighlight>
 
=={{header|Clojure}}==
A simple approach:
<lang clojure>(defn leap-year? [y]
<syntaxhighlight lang="clojure">(defn leap-year? [y]
(and (zero? (mod y 4)) (or (pos? (mod y 100)) (zero? (mod y 400)))))</lang>
(and (zero? (mod y 4))
(or (pos? (mod y 100))
(zero? (mod y 400)))))
</syntaxhighlight>
A slightly terser, if slightly less obvious approach:
<syntaxhighlight lang="clojure">(defn leap-year? [y]
(condp #(zero? (mod %2 %1)) y
400 true
100 false
4 true
false))
</syntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">is_leap_year = proc (year: int) returns (bool)
return(year//400 =0 cor (year//4 = 0 cand year//100 ~= 0))
end is_leap_year
 
start_up = proc ()
po: stream := stream$primary_output()
years: sequence[int] := sequence[int]$
[1899, 1900, 1901, 1902, 1903, 1904, 1905, 1999,
2000, 2001, 2002, 2003, 2004, 2021, 2022]
for year: int in sequence[int]$elements(years) do
stream$puts(po, int$unparse(year) || " is ")
if ~is_leap_year(year) then stream$puts(po, "not ") end
stream$putl(po, "a leap year.")
end
end start_up</syntaxhighlight>
{{out}}
<pre>1899 is not a leap year.
1900 is not a leap year.
1901 is not a leap year.
1902 is not a leap year.
1903 is not a leap year.
1904 is a leap year.
1905 is not a leap year.
1999 is not a leap year.
2000 is a leap year.
2001 is not a leap year.
2002 is not a leap year.
2003 is not a leap year.
2004 is a leap year.
2021 is not a leap year.
2022 is not a leap year.</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. leap-year.
 
Line 818 ⟶ 1,525:
 
GOBACK
.</langsyntaxhighlight>
 
Using Date Intrinsic Functions
<syntaxhighlight lang="cobol">
<lang COBOL>
program-id. leap-yr.
*> Given a year, where 1601 <= year <= 9999
Line 855 ⟶ 1,562:
.
end program leap-yr.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 873 ⟶ 1,580:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun leap-year-p (year)
(destructuring-bind (fh h f)
(mapcar #'(lambda (n) (zerop (mod year n))) '(400 100 4))
(or fh (and (not h) f))))</langsyntaxhighlight>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE LeapYear;
IMPORT StdLog, Strings, Args;
Line 910 ⟶ 1,617:
END Do;
END LeapYear.
</syntaxhighlight>
</lang>
Execute: ^Q LeapYear.Do 2000 2004 2013~<br/>
{{out}}
Line 921 ⟶ 1,628:
=={{header|Crystal}}==
 
<langsyntaxhighlight lang="ruby">p Time.leap_year?(2020)
p Time.leap_year?(2021)
p Time.leap_year?(2022)</langsyntaxhighlight>
 
<pre>
Line 932 ⟶ 1,639:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.algorithm;
 
bool leapYear(in uint y) pure nothrow {
Line 944 ⟶ 1,651:
1973, 2100, 2107, 2200, 2203, 2289];
assert(filter!leapYear(bad ~ good).equal(good));
}</langsyntaxhighlight>
 
 
Using the datetime library:
<langsyntaxhighlight lang="d">import std.datetime;
 
void main() {
Line 956 ⟶ 1,663:
assert(DateTime(2000, 1, 1).isLeapYear);
}
</syntaxhighlight>
</lang>
 
=={{header|Dart}}==
 
<langsyntaxhighlight Dartlang="dart">class Leap {
bool leapYear(num year) {
return (year % 400 == 0) || (( year % 100 != 0) && (year % 4 == 0));
Line 968 ⟶ 1,675:
// Source: https://api.flutter.dev/flutter/quiver.time/isLeapYear.html
}
}</langsyntaxhighlight>
 
=={{header|Dc}}==
Directly taken from Wikipedia.
{{works with|GNU dc}}
<langsyntaxhighlight Dclang="dc">[0q]s0
[1q]s1
 
Line 996 ⟶ 1,703:
1989 lTx
1900 lTx
2000 lTx</langsyntaxhighlight>
{{out}}
<pre>
Line 1,007 ⟶ 1,714:
=={{header|Delphi}}/{{header|Pascal}}==
Delphi has standard function IsLeapYear in SysUtils unit.
<langsyntaxhighlight Delphilang="delphi">program TestLeapYear;
 
{$APPTYPE CONSOLE}
Line 1,025 ⟶ 1,732:
Writeln(Year, ' is not a Leap year');
Readln;
end.</langsyntaxhighlight>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec leap_year(word year) bool:
year%400=0 or (year%4=0 and year%100/=0)
corp
 
proc nonrec main() void:
[15]word years = (1899, 1900, 1901, 1902, 1903, 1904, 1905, 1999,
2000, 2001, 2002, 2003, 2004, 2021, 2022);
word i;
for i from 0 upto 14 do
writeln(years[i],
if leap_year(years[i]) then " is " else " is not " fi,
"a leap year.")
od
corp</syntaxhighlight>
{{out}}
<pre>1899 is not a leap year.
1900 is not a leap year.
1901 is not a leap year.
1902 is not a leap year.
1903 is not a leap year.
1904 is a leap year.
1905 is not a leap year.
1999 is not a leap year.
2000 is a leap year.
2001 is not a leap year.
2002 is not a leap year.
2003 is not a leap year.
2004 is a leap year.
2021 is not a leap year.
2022 is not a leap year.</pre>
 
=={{header|DWScript}}==
<langsyntaxhighlight Delphilang="delphi">function IsLeapYear(y : Integer) : Boolean;
begin
Result:= (y mod 4 = 0)
Line 1,048 ⟶ 1,788:
PrintLn('Checking non-leap years');
for i in bad do
if IsLeapYear(i) then PrintLn(i);</langsyntaxhighlight>
 
=={{header|Dyalect}}==
 
<langsyntaxhighlight Dyalectlang="dyalect">func isLeap(y) {
if y % 100 == 0 {
y % 400 == 0
Line 1,060 ⟶ 1,800:
}
 
print(isLeap(1984))</langsyntaxhighlight>
 
{{out}}
 
<pre>true</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func leapyear y .
if y mod 4 = 0 and (y mod 100 <> 0 or y mod 400 = 0)
return 1
.
return 0
.
print leapyear 2000
</syntaxhighlight>
 
=={{header|Ela}}==
<langsyntaxhighlight lang="ela">isLeap y | y % 100 == 0 = y % 400 == 0
| else = y % 4 == 0</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">leap_year? = fn(year) -> :calendar.is_leap_year(year) end
IO.inspect for y <- 2000..2020, leap_year?.(y), do: y</langsyntaxhighlight>
 
{{out}}
Line 1,081 ⟶ 1,832:
=={{header|Emacs Lisp}}==
{{trans|Scheme}}
<langsyntaxhighlight lang="lisp">(defun leap-year-p (year)
(apply (lambda (a b c) (or a (and (not b) c)))
(mapcar (lambda (n) (zerop (mod year n)))
'(400 100 4))))</langsyntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
-module(gregorian).
-export([leap/1]).
 
leap( Year ) -> calendar:is_leap_year( Year ).
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM LEAP_YEAR
 
FUNCTION LEAP(YR%)
Line 1,111 ⟶ 1,862:
END IF
END LOOP
END PROGRAM</langsyntaxhighlight>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function isLeapYear(integer year)
return remainder(year,4)=0 and remainder(year,100)!=0 or remainder(year,400)=0
end function</langsyntaxhighlight>
 
=={{header|Excel}}==
Take two cells, say A1 and B1, in B1 type in :
 
<syntaxhighlight lang="excel">
<lang Excel>
=IF(OR(NOT(MOD(A1,400)),AND(NOT(MOD(A1,4)),MOD(A1,100))),"Leap Year","Not a Leap Year")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,133 ⟶ 1,884:
2012 Leap Year
</pre>
 
===LAMBDA===
 
Binding the name ISLEAPYEAR to the following lambda expression in the Name Manager of the Excel WorkBook,
 
as a reusable custom function:
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">ISLEAPYEAR
=LAMBDA(y,
OR(
0 = MOD(y, 400),
AND(
0 = MOD(y, 4),
0 <> MOD(y, 100)
)
)
)</syntaxhighlight>
 
{{Out}}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="2" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=ISLEAPYEAR(A2)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="text-align:left; font-style:italic" | Year
| style="text-align:left; font-style:italic" | Verdict
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:left" | 1900
| style="text-align:left; background-color:#cbcefb" | FALSE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
| style="text-align:left" | 1954
| style="text-align:left" | FALSE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
| style="text-align:left" | 1996
| style="text-align:left" | TRUE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
| style="text-align:left" | 2003
| style="text-align:left" | FALSE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
| style="text-align:left" | 2012
| style="text-align:left" | TRUE
|}
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let isLeapYear = System.DateTime.IsLeapYear
assert isLeapYear 1996
assert isLeapYear 2000
assert not (isLeapYear 2001)
assert not (isLeapYear 1900)</langsyntaxhighlight>
 
=={{header|Factor}}==
Call ''leap-year?'' word from ''calendars'' vocabulary. For example:
<langsyntaxhighlight lang="factor">USING: calendar prettyprint ;
2011 leap-year? .</langsyntaxhighlight>
Factor uses proleptic Gregorian calendar.
 
=={{header|Fermat}}==
<syntaxhighlight lang="text">Function IsLeap(y) = if y|4>0 then 0 else if y|100=0 and y|400>0 then 0 else 1 fi fi.</syntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: leap-year? ( y -- ? )
dup 400 mod 0= if drop true exit then
dup 100 mod 0= if drop false exit then
4 mod 0= ;</langsyntaxhighlight>
 
Or more simply (but always computing three "mod"):
<langsyntaxhighlight lang="forth">: leap-year? dup 4 mod 0= over 16 mod 0= rot 25 mod 0= not or and ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">program leap
implicit none
 
Line 1,173 ⟶ 1,982:
end function leap_year
end program leap</langsyntaxhighlight>
{{out}}
<pre> F T F T </pre>
 
=={{header|FreeBASICFōrmulæ}}==
<lang FreeBASIC>' version 23-06-2015
' compile with: fbc -s console
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Leap_year}}
#Ifndef TRUE ' define true and false for older freebasic versions
#Define FALSE 0
#Define TRUE Not FALSE
#EndIf
 
'''Solution'''
Function leapyear(Year_ As Integer) As Integer
 
[[File:Fōrmulæ - Leap year 01.png]]
If (Year_ Mod 4) <> 0 Then Return FALSE
If (Year_ Mod 100) = 0 AndAlso (Year_ Mod 400) <> 0 Then Return FALSE
Return TRUE
 
In a more concise way:
End Function
 
[[File:Fōrmulæ - Leap year 02.png]]
' ------=< MAIN >=------
 
[[File:Fōrmulæ - Leap year 03.png]]
' year is a FreeBASIC keyword
Dim As Integer Year_
 
[[File:Fōrmulæ - Leap year 04.png]]
For Year_ = 1800 To 2900 Step 100
Print Year_; IIf(leapyear(Year_), " is a leap year", " is not a leap year")
Next
 
Print : Print
 
For Year_ = 2012 To 2031
Print Year_;
If leapyear(Year_) = TRUE Then
Print " = leap",
Else
Print " = no",
End If
If year_ Mod 4 = 3 Then Print ' lf/cr
Next
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre> 1800 is not a leap year
1900 is not a leap year
2000 is a leap year
2100 is not a leap year
2200 is not a leap year
2300 is not a leap year
2400 is a leap year
2500 is not a leap year
2600 is not a leap year
2700 is not a leap year
2800 is a leap year
2900 is not a leap year
 
2012 = leap 2013 = no 2014 = no 2015 = no
2016 = leap 2017 = no 2018 = no 2019 = no
2020 = leap 2021 = no 2022 = no 2023 = no
2024 = leap 2025 = no 2026 = no 2027 = no
2028 = leap 2029 = no 2030 = no 2031 = no</pre>
 
=={{header|FutureBasic}}==
<lang futurebasic>
include "ConsoleWindow"
 
// In-line C function to generate random number in range
BeginCFunction
long randomInRange( long min, long max ) {
int i = (arc4random()%(max-min+1))+min;
return (long)i;
}
EndC
toolbox fn randomInRange( long min, long max ) = long
 
// Leap year test function
local fn LeapYear( year as long ) as Boolean
dim as Boolean result : result = _false
 
if year mod 400 == 0 then result = _true : exit fn
if year mod 100 == 0 then result = _false : exit fn
if year mod 4 == 0 then result = _true : exit fn
if year mod 4 != 0 then result = _false : exit fn
end fn = result
 
dim as long i, y, knownLeapYear(10)
 
// Array of known leap years from 1980 through 2020 for control
knownLeapYear(0) = 1980 : knownLeapYear(1) = 1984 : knownLeapYear(2) = 1988
knownLeapYear(3) = 1992 : knownLeapYear(4) = 1996 : knownLeapYear(5) = 2000
knownLeapYear(6) = 2004 : knownLeapYear(7) = 2008 : knownLeapYear(8) = 2012
knownLeapYear(9) = 2016 : knownLeapYear(10) = 2020
 
print "Known leap years:"
for i = 0 to 9
if ( fn LeapYear( knownLeapYear(i) ) == _true )
print knownLeapYear(i); " is a leap year."
else
print knownLeapYear(i); " is a not leap year."
end if
next
 
print
 
// Random years from 1980 to 2020 to test
print "Check random years:"
for i = 0 to 20
y = fn randomInRange( 1980, 2020 )
if ( fn LeapYear( y ) == _true )
print y; " is a leap year."
else
print y; " is a not leap year."
end if
next
</lang>
 
Output (results will vary for random years):
<pre>
Known leap years:
1980 is a leap year.
1984 is a leap year.
1988 is a leap year.
1992 is a leap year.
1996 is a leap year.
2000 is a leap year.
2004 is a leap year.
2008 is a leap year.
2012 is a leap year.
2016 is a leap year.
 
Check random years:
1998 is a not leap year.
1987 is a not leap year.
2015 is a not leap year.
1998 is a not leap year.
2020 is a leap year.
2020 is a leap year.
2009 is a not leap year.
2020 is a leap year.
2018 is a not leap year.
2013 is a not leap year.
2003 is a not leap year.
1994 is a not leap year.
1989 is a not leap year.
1999 is a not leap year.
1984 is a leap year.
1980 is a leap year.
1998 is a not leap year.
2008 is a leap year.
1983 is a not leap year.
2007 is a not leap year.
2004 is a leap year.
</pre>
 
=={{header|Gambas}}==
<lang gambas>Public Sub Form_Open()
Dim dDate As Date
Dim siYear As Short = InputBox("Enter a year", "Leap year test")
Dim sMessage As String = " is a leap year."
 
Try dDate = Date(siYear, 02, 29)
If Error Then sMessage = " is not a leap year."
 
Message(siYear & sMessage)
 
End</lang>
 
Output:
<pre>
2016 is a leap year.
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">IsLeapYear := function(n)
return (n mod 4 = 0) and ((n mod 100 <> 0) or (n mod 400 = 0));
end;
Line 1,358 ⟶ 2,010:
IsLeapYear := function(n)
return DaysInYear(n) = 366;
end;</langsyntaxhighlight>
 
=={{header|Genie}}==
Dialect conversion from Vala entry.
 
<langsyntaxhighlight lang="genie">[indent=4]
/*
Leap year, in Genie
Line 1,375 ⟶ 2,027:
for year in years
status:string = year.is_leap_year() ? "" : "not "
stdout.printf("%d is %sa leap year.\n", year, status)</langsyntaxhighlight>
 
{{out}}
Line 1,388 ⟶ 2,040:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">func isLeap(year int) bool {
return year%400 == 0 || year%4 == 0 && year%100 != 0
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">(1900..2012).findAll {new GregorianCalendar().isLeapYear(it)}.each {println it}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll;">1904
Line 1,424 ⟶ 2,076:
2008
2012</pre>
 
=={{header|GW-BASIC}}==
{{works with|PC-BASIC|any}}
<lang qbasic>
10 ' Leap year
20 DEF FN ISLEAPYEAR(Y%) = ((Y% MOD 4 = 0) AND (Y% MOD 100 <> 0)) OR (Y% MOD 400 = 0)
95 ' *** Test ***
100 FOR I% = 1 TO 5
110 READ YEAR%
120 PRINT YEAR%; "is ";
130 IF FN ISLEAPYEAR(YEAR%) = 0 THEN PRINT "not "; ELSE PRINT "";
140 PRINT "a leap year."
150 NEXT I%
160 END
200 DATA 1900, 1994, 1996, 1997, 2000
</lang>
{{out}}
<pre>
1900 is not a leap year.
1994 is not a leap year.
1996 is a leap year.
1997 is not a leap year.
2000 is a leap year.
</pre>
 
=={{header|Harbour}}==
<langsyntaxhighlight lang="visualfoxpro">FUNCTION IsLeapYear( nYear )
RETURN iif( nYear % 100 == 0, nYear % 400 == 0, nYear % 4 == 0 )</langsyntaxhighlight>
 
=={{header|Haskell}}==
'''Simple version'''
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Monad
import Control.Arrow
Line 1,463 ⟶ 2,091:
 
isleapsf j | 0==j`mod`100 = 0 == j`mod`400
| otherwise = 0 == j`mod`4</langsyntaxhighlight>
'''Algorithmic'''
<langsyntaxhighlight lang="haskell">isleap = foldl1 ((&&).not).flip map [400, 100, 4]. ((0==).).mod</langsyntaxhighlight>
Example using isleap
<langsyntaxhighlight lang="haskell">*Main> mapM_ (putStrLn. (ap leaptext isleap)) [1900,1994,1996,1997,2000]
1900 is not a leap year
1994 is not a leap year
1996 is a leap year
1997 is not a leap year
2000 is a leap year</langsyntaxhighlight>
 
'''TDD version'''
<langsyntaxhighlight lang="haskell">import Test.HUnit
 
isLeapYear::Int->Bool
Line 1,488 ⟶ 2,116:
,TestCase $ assertEqual "64 is a leap year" True $ isLeapYear 64
,TestCase $ assertEqual "2000 is a leap year" True $ isLeapYear 2000
,TestCase $ assertEqual "1900 is not a leap year" False $ isLeapYear 1900]</langsyntaxhighlight>
 
=={{header|Hy}}==
<langsyntaxhighlight lang="clojure">(defn leap? [y]
(and
(= (% y 4) 0)
(or
(!= (% y 100) 0)
(= (% y 400) 0))))</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Gives leap year status for 2000,1900,2012 and any arguments you give
<langsyntaxhighlight Iconlang="icon">procedure main(arglist)
every y := !([2000,1900,2012]|||arglist) do
write("The year ",y," is ", leapyear(y) | "not ","a leap year.")
Line 1,507 ⟶ 2,135:
procedure leapyear(year) #: determine if year is leap
if (numeric(year) % 4 = 0 & year % 100 ~= 0) | (numeric(year) % 400 = 0) then return
end</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">isLeap=: 0 -/@:= 4 100 400 |/ ]</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang="j"> isLeap 1900 1996 1997 2000
0 1 0 1</langsyntaxhighlight>
 
=={{header|Java}}==
Line 1,521 ⟶ 2,149:
Both values are printed in the output.
 
<langsyntaxhighlight lang="java">import java.util.GregorianCalendar;
import java.text.MessageFormat;
 
Line 1,539 ⟶ 2,167:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>The year 1800 is leaper: false / false.
Line 1,553 ⟶ 2,181:
{{works with|Java|8}}
 
<langsyntaxhighlight lang="java">import java.time.Year;
 
public class IsLeap {
Line 1,561 ⟶ 2,189:
}
}
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">var isLeapYear = function (year) { return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); };</langsyntaxhighlight>
Or, by setting the day to the 29th and checking if the day remains
<langsyntaxhighlight lang="javascript">// Month values start at 0, so 1 is for February
var isLeapYear = function (year) { return new Date(year, 1, 29).getDate() === 29; };</langsyntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE leapyear == dup 100 div null rotate choice 4 rem null.</syntaxhighlight>
 
=={{header|jq}}==
{{trans|Julia}}
<langsyntaxhighlight lang="jq">def leap:
. as $y | ($y%4) == 0 and ($y < 1582 or ($y%400) == 0 or ($y%100) != 0);</langsyntaxhighlight>
'''Examples''':
<langsyntaxhighlight lang="jq">def assert(value; f):
value as $value
| ($value|f) | if . then empty else error("assertion violation: \($value) => \(.)") end;
Line 1,581 ⟶ 2,212:
 
((2100, 2014, 1900, 1800, 1700, 1499) | assert(.; leap|not))
</syntaxhighlight>
</lang>
{{out}}
$ jq -n -f Leap_year.jq
Line 1,588 ⟶ 2,219:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">isleap(yr::Integer) = yr % 4 == 0 && (yr < 1582 || yr % 400 == 0 || yr % 100 != 0)
 
@assert all(isleap, [2400, 2012, 2000, 1600, 1500, 1400])
@assert !any(isleap, [2100, 2014, 1900, 1800, 1700, 1499])</langsyntaxhighlight>
 
=={{header|K}}==
=== K3 ===
<lang K> leapyear:{(+/~x!'4 100 400)!2}
{{works with|Kona}}
Leap year predicate:
<syntaxhighlight lang="k"> lyp:{(+/~x!'4 100 400)!2}
 
lyp'1996+!6
1 0 0 0 1 0</syntaxhighlight>
Leap year selection:
<syntaxhighlight lang="k"> lys:{a@&lyp'a:x}
 
lys@1900,1994,1996,1997,2000
1996 2000</syntaxhighlight>
 
=={{header|Koka}}==
Chain of boolean expressions
<syntaxhighlight lang="koka">
pub fun is-leap-year(year: int)
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)</syntaxhighlight>
 
If-Then-Else
<syntaxhighlight lang="koka">
pub fun is-leap-year'(year: int)
year % (if year % 100 == 0 then 400 else 4) == 0</syntaxhighlight>
 
This approach use the buit-in libraries to create the february 28th date and the adds a day to it, which if it's in a leap year the next day wil be the 29th.
a@&leapyear'a:1900,1994,1996,1997,2000
<syntaxhighlight lang="koka"> import std/time
1996 2000</lang>
import std/time/date
import std/time/time
 
pub fun is-leap-year''(year: int)
Date(year, 2, 28).time.add-days(1).day == 29</syntaxhighlight>
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">fun isLeapYear(year: Int) = year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define isLeapYear(y::integer) => {
#y % 400 == 0 ? return true
#y % 100 == 0 ? return false
Line 1,613 ⟶ 2,270:
isLeapYear(#test)
'\r'
^}</langsyntaxhighlight>
 
{{out}}
Line 1,623 ⟶ 2,280:
true</pre>
 
=={{header|Liberty BASIC}}==
=== Simple method ===
<lang lb>if leap(1996)then
print "leap"
else
print "ordinary"
end if
wait
 
function leap(n)
leap=date$("2/29/";n)
end function</lang>
 
=== Calculated method ===
<lang lb> year = 1908
select case
case year mod 400 = 0
leapYear = 1
case year mod 4 = 0 and year mod 100 <> 0
leapYear = 1
case else
leapYear = 0
end select
if leapYear = 1 then
print year;" is a leap year."
else
print year;" is not a leap year."
end if</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on isLeapYear (year)
return date(year, 2, 29).month=2
end</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function isLeapYear year
return (year MOD 4 is 0) AND ((year MOD 400 is 0) OR (year MOD 100 is not 0))
end isLeapYear
Line 1,675 ⟶ 2,304:
1996 is true
1997 is false
2000 is true </langsyntaxhighlight>
 
=={{header|LLVM}}==
<langsyntaxhighlight lang="llvm">; This is not strictly LLVM, as it uses the C library function "printf".
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 1,771 ⟶ 2,400:
 
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { argmemonly nounwind }</langsyntaxhighlight>
{{out}}
<pre>1900 is not a leap year.
Line 1,780 ⟶ 2,409:
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to multiple? :n :d
output equal? 0 modulo :n :d
end
to leapyear? :y
output ifelse multiple? :y 100 [multiple? :y 400] [multiple? :y 4]
end</langsyntaxhighlight>
 
=={{header|Logtalk}}==
<langsyntaxhighlight lang="logtalk">leap_year(Year) :-
( mod(Year, 4) =:= 0, mod(Year, 100) =\= 0 ->
true
; mod(Year, 400) =:= 0
).</langsyntaxhighlight>
 
=={{header|LOLCODE}}==
<langsyntaxhighlight lang="lolcode">BTW Determine if a Gregorian calendar year is leap
HAI 1.3
HOW IZ I Leap YR Year
Line 1,841 ⟶ 2,470:
 
KTHXBYE
</syntaxhighlight>
</lang>
{{Out}}
<pre>1900 is NOT a leap year
Line 1,852 ⟶ 2,481:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function isLeapYear(year)
return year%4==0 and (year%100~=0 or year%400==0)
end</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">isLeapYear := proc(year)
if not year mod 4 = 0 or (year mod 100 = 0 and not year mod 400 = 0) then
return false;
Line 1,863 ⟶ 2,492:
return true;
end if;
end proc:</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Dates are handled by built-in functions in the Wolfram Language
<syntaxhighlight lang Mathematica="mathematica">LeapYearQ[2002]</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
MATLAB, conveniently, provides a function that returns the last day of an arbitrary month of the calendar given the year. Using the fact that February is 29 days long during a leap year, we can write a one-liner that solves this task.
<langsyntaxhighlight MATLABlang="matlab">function TrueFalse = isLeapYear(year)
TrueFalse = (eomday(year,2) == 29);
end</langsyntaxhighlight>
 
===Using Logical and modular functions===
<langsyntaxhighlight lang="matlab">x = ~mod(YEAR, 4) & (mod(YEAR, 100) | ~mod(YEAR, 400))</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">leapyearp(year) := is(mod(year, 4) = 0 and
(mod(year, 100) # 0 or mod(year, 400) = 0))$</langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight lang="mercury">:- pred is_leap_year(int::in) is semidet.
 
is_leap_year(Year) :-
( if Year mod 100 = 0 then Year mod 400 = 0 else Year mod 4 = 0 ).</langsyntaxhighlight>
 
Usage:
 
<langsyntaxhighlight lang="mercury">:- module leap_year.
:- interface.
 
Line 1,907 ⟶ 2,536:
write_year_kind(Year, !IO) :-
io.format("%d %s a leap year.\n",
[i(Year), s(if is_leap_year(Year) then "is" else "is not" )], !IO).</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">(mod 0 ==) :divisor?
(((400 divisor?) (4 divisor?) (100 divisor? not)) cleave and or) :leap-year?</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">isLeapYear = function(year)
return year%4==0 and (year % 100 or not year % 400)
end function</langsyntaxhighlight>
 
=={{header|MIPS Assembly}}==
Pass year in a0, returns boolean in v0.
<langsyntaxhighlight lang="mips">
IsLeap: andi $a1, $a0, 3 #a0 is year to test
bnez $a1 NotLeap
Line 1,935 ⟶ 2,564:
NotLeap:li $v0, 0
jr $ra
</syntaxhighlight>
</lang>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П0 1 0 0 / {x} x=0 14 ИП0 4
0 0 ПП 18 ИП0 4 ПП 18 / {x}
x=0 24 1 С/П 0 С/П</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE LeapYear;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 1,972 ⟶ 2,601:
Print(2000);
ReadChar
END LeapYear.</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">ILY(X) ;IS IT A LEAP YEAR?
QUIT ((X#4=0)&(X#100'=0))!((X#100=0)&(X#400=0))</langsyntaxhighlight>
Usage: <pre>USER>W $SELECT($$ILY^ROSETTA(1900):"Yes",1:"No")
No
Line 1,986 ⟶ 2,615:
=={{header|Nanoquery}}==
{{trans|Python}}
<langsyntaxhighlight Nanoquerylang="nanoquery">def isLeapYear(year)
if (year % 100 = 0)
return (year % 400 = 0)
Line 1,992 ⟶ 2,621:
return (year % 4 = 0)
end
end</langsyntaxhighlight>
 
=={{header|Neko}}==
Translating from C
 
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
<doc><h2>Leap year, in Neko</h2></doc>
**/
Line 2,005 ⟶ 2,634:
var tests = $array(2000, 1997, 1996, 1994, 1990, 1980, 1900)
var cnt = $asize(tests)
while (cnt -= 1) >= 0 $print(tests[cnt], if leapyear(tests[cnt]) " is" else " is not", " a leapyear", "\n")</langsyntaxhighlight>
 
{{out}}
Line 2,020 ⟶ 2,649:
=={{header|Nemerle}}==
Demonstrating implementation as well as use of standard library function.
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
using Nemerle.Assertions;
Line 2,056 ⟶ 2,685:
DateTime.IsLeapYear(DateTime.Now.Year));
}
}</langsyntaxhighlight>
{{out}}
<pre>2000 is a leap year: True
Line 2,073 ⟶ 2,702:
prior to the Gregorian cut-over and leap-year rules in the Julian calendar
are different to those for the Gregorian calendar.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols nobinary
Line 2,117 ⟶ 2,746:
 
method isFalse public constant binary returns boolean
return \isTrue</langsyntaxhighlight>
{{out}}
<pre>
Line 2,128 ⟶ 2,757:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import times
let year = 1980
echo isLeapYear(year)
Line 2,134 ⟶ 2,763:
# or
 
proc isLeapYear2(year: Natural): bool =
if year mod 100 == 0:
year mod 400 == 0
else: year mod 4 == 0
 
echo isLeapYear2(year)</langsyntaxhighlight>
{{out}}
<pre>true
true</pre>
 
=={{header|NS-HUBASIC}}==
<lang NS-HUBASIC>10 INPUT "ENTER A NUMBER, AND I'LL DETECT IF IT'S A LEAP YEAR OR NOT. ",A
20 IF A-(A/100)*100=0 AND A-(A/400)*400<>0 THEN RESULT$="NOT "
30 PRINT "THAT'S "RESULT$"A LEAP YEAR."</lang>
 
=={{header|Oberon-2}}==
<langsyntaxhighlight lang="oberon2">
PROCEDURE IsLeapYear(year: INTEGER): BOOLEAN;
BEGIN
Line 2,167 ⟶ 2,791:
END
END IsLeapYear;
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class LeapYear {
function : Main(args : String[]) ~ Nil {
Line 2,196 ⟶ 2,820:
}
}
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let is_leap_year ~year =
ifyear mod (if year mod 100 = 0 then 400 else 4) = 0</syntaxhighlight>
then (year mod 400) = 0
else (year mod 4) = 0</lang>
Using Unix Time functions:
<langsyntaxhighlight lang="ocaml">let is_leap_year ~year =
let tm =
Unix.mktime {
Line 2,213 ⟶ 2,835:
}
in
(tm.Unix.tm_mday = 29)</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang Oforth="oforth">Date.IsLeapYear(2000)</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
::routine isLeapYear
use arg year
d = .datetime~new(year, 1, 1)
return d~isLeapYear
</syntaxhighlight>
</lang>
 
=={{header|OpenEdge/Progress}}==
The DATE function converts month, day, year integers to a date data type and will set the error status if invalid values are passed.
<langsyntaxhighlight lang="progress">FUNCTION isLeapYear RETURNS LOGICAL (
i_iyear AS INTEGER
):
Line 2,244 ⟶ 2,866:
1997 isLeapYear( 1997 ) SKIP
2000 isLeapYear( 2000 )
VIEW-AS ALERT-BOX.</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {IsLeapYear Year}
case Year mod 100 of 0 then
Line 2,262 ⟶ 2,884:
{System.showInfo Y#" is NOT a leap year."}
end
end</langsyntaxhighlight>
{{out}}
<pre>1900 is NOT a leap year.
Line 2,270 ⟶ 2,892:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">isLeap(n)={
if(n%400==0, return(1));
if(n%100==0, return(0));
n%4==0
};</langsyntaxhighlight>
 
Alternate version:
<langsyntaxhighlight lang="parigp">isLeap(n)=!(n%if(n%100,4,400))</langsyntaxhighlight>
 
{{works with|PARI/GP|2.6.0 and above}}
<langsyntaxhighlight lang="parigp">isLeap(n)={
if(n%4,0,
n%100,1,
n%400,0,1
)
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
<langsyntaxhighlight lang="pascal">program LeapYear;
uses
sysutils;//includes isLeapYear
Line 2,305 ⟶ 2,927:
TestYear(2100);
TestYear(1904);
end.</langsyntaxhighlight>
Output:
<pre>1900 is NO leap year
Line 2,313 ⟶ 2,935:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub isleap {
my $year = shift;
if ($year % 100 == 0) {
Line 2,319 ⟶ 2,941:
}
return ($year % 4 == 0);
}</langsyntaxhighlight>
 
Or more concisely:
 
<langsyntaxhighlight Perllang="perl">sub isleap { !(not $_[0] % 100) ? !($_[0] % 400)100 :? !($_[0]4 %: 4400) }</langsyntaxhighlight>
 
Alternatively, using functions/methods from CPAN modules:
 
<langsyntaxhighlight Perllang="perl">use Date::Manip;
print Date_LeapYear(2000);
 
Line 2,336 ⟶ 2,958:
use DateTime;
my $date = DateTime->new(year => 2000);
print $date->is_leap_year();</langsyntaxhighlight>
 
=={{header|Phix}}==
Available as an auto-include, implemented as:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>global function is_leap_year(integer y)
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #7060A8;">is_leap_year</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
return remainder(y,4)=0 and (remainder(y,100)!=0 or remainder(y,400)=0)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">400</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
end function</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</syntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
function isLeapYear($year) {
if ($year % 100 == 0) {
Line 2,351 ⟶ 2,975:
}
return ($year % 4 == 0);
}</langsyntaxhighlight>
With <code>date('L')</code>:
<langsyntaxhighlight lang="php"><?php
function isLeapYear($year) {
return (date('L', mktime(0, 0, 0, 2, 1, $year)) === '1')
}</langsyntaxhighlight>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
foreach(Y in [1600,1700,1899,1900,2000,2006,2012])
println(Y=cond(leap_year(Y),leap_year,not_leap_year))
end,
nl.
 
leap_year(Year) =>
(Year mod 4 == 0, Year mod 100 != 0)
;
Year mod 400 == 0. </syntaxhighlight>
 
{{out}}
<pre>1600 = leap_year
1700 = not_leap_year
1899 = not_leap_year
1900 = not_leap_year
2000 = leap_year
2006 = not_leap_year
2012 = leap_year</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de isLeapYear (Y)
(bool (date Y 2 29)) )</langsyntaxhighlight>
{{out}}
<pre>: (isLeapYear 2010)
Line 2,373 ⟶ 3,018:
: (isLeapYear 1700)
-> NIL</pre>
 
=={{header|PL/0}}==
{{trans|Tiny BASIC}}
<syntaxhighlight lang="pascal">
var isleap, year;
 
procedure checkifleap;
begin
isleap := 0;
if (year / 4) * 4 = year then
begin
if year - (year / 100) * 100 <> 0 then isleap := 1;
if year - (year / 400) * 400 = 0 then isleap := 1
end;
end;
 
begin
year := 1759;
while year <= 2022 do
begin
call checkifleap;
if isleap = 1 then ! year;
year := year + 1
end
end.
</syntaxhighlight>
{{out}}
<pre>
1760
1764
1768
1772
1776
1780
1784
1788
1792
1796
1804
1808
1812
1816
1820
1824
1828
1832
1836
1840
1844
1848
1852
1856
1860
1864
1868
1872
1876
1880
1884
1888
1892
1896
1904
1908
1912
1916
1920
1924
1928
1932
1936
1940
1944
1948
1952
1956
1960
1964
1968
1972
1976
1980
1984
1988
1992
1996
2000
2004
2008
2012
2016
2020
</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">dcl mod builtin;
dcl year fixed bin (31);
 
Line 2,385 ⟶ 3,123:
else
put skip edit(year, 'is not a leap year') (p'9999b', a);
end;</langsyntaxhighlight>
 
{{out}}
Line 2,396 ⟶ 3,134:
2000 is a leap year
2001 is not a leap year
</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="pli">100H: /* DETERMINE WHETHER SOME YEARS ARE LEAP YEARS OR NOT */
 
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5;END;
/* CONSOLE OUTPUT ROUTINES */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE INITIAL( '.....$' ), W BYTE;
N$STR( W := LAST( N$STR ) - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* TASK */
/* RETURNS TRUE IF YEAR IS A LEAP YEAR, FALSE OTHERWISE */
/* ASSUMES YEAR IS IN THE GREGORIAN CALENDAR */
IS$LEAP$YEAR: PROCEDURE( YEAR )BYTE;
DECLARE YEAR ADDRESS;
RETURN ( YEAR MOD 400 = 0
OR ( YEAR MOD 4 = 0 AND YEAR MOD 100 <> 0 )
);
END IS$LEAPYEAR ;
/* TEST CASES */
DECLARE TEST$YEAR ( 15 )ADDRESS INITIAL( 1899, 1900, 1901, 1902, 1903
, 1904, 1905, 1999, 2000, 2001
, 2002, 2003, 2004, 2021, 2022
);
DECLARE Y$POS BYTE;
DO Y$POS = 0 TO LAST( TEST$YEAR );
CALL PR$NUMBER( TEST$YEAR( Y$POS ) );
CALL PR$STRING( .' IS $' );
IF NOT IS$LEAP$YEAR( TEST$YEAR( Y$POS ) ) THEN DO;
CALL PR$STRING( .'NOT $' );
END;
CALL PR$STRING( .'A LEAP YEAR$' );
CALL PR$NL;
END;
 
EOF</syntaxhighlight>
{{out}}
<pre>
1899 IS NOT A LEAP YEAR
1900 IS NOT A LEAP YEAR
1901 IS NOT A LEAP YEAR
1902 IS NOT A LEAP YEAR
1903 IS NOT A LEAP YEAR
1904 IS A LEAP YEAR
1905 IS NOT A LEAP YEAR
1999 IS NOT A LEAP YEAR
2000 IS A LEAP YEAR
2001 IS NOT A LEAP YEAR
2002 IS NOT A LEAP YEAR
2003 IS NOT A LEAP YEAR
2004 IS A LEAP YEAR
2021 IS NOT A LEAP YEAR
2022 IS NOT A LEAP YEAR
</pre>
 
=={{header|PostScript}}==
<langsyntaxhighlight lang="postscript">/isleapyear {
dup dup
4 mod 0 eq % needs to be divisible by 4
Line 2,408 ⟶ 3,210:
400 mod 0 eq % or by 400
or
} def</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">$Year = 2016
[System.DateTime]::IsLeapYear( $Year )</langsyntaxhighlight>
 
=={{header|Prolog}}==
{{Works with|SWI-Prolog}}
<langsyntaxhighlight Prologlang="prolog">leap_year(L) :-
partition(is_leap_year, L, LIn, LOut),
format('leap years : ~w~n', [LIn]),
Line 2,425 ⟶ 3,227:
R100 is Year mod 100,
R400 is Year mod 400,
( (R4 = 0, R100 \= 0); R400 = 0).</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight Prologlang="prolog"> ?- leap_year([1900,1994,1996,1997,2000 ]).
leap years : [1996,2000]
not leap years : [1900,1994,1997]
L = [1900,1994,1996,1997,2000].</langsyntaxhighlight>
 
There is an handy builtin that simplifies a lot, ending up in a simple query:
 
<syntaxhighlight lang="prolog">
<lang Prolog>
?- findall(Y, (between(1990,2030,Y),day_of_the_year(date(Y,12,31),366)), L).
L = [1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028].
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure isLeapYear(Year)
If (Year%4=0 And Year%100) Or Year%400=0
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import calendar
calendar.isleap(year)</langsyntaxhighlight>
or
<langsyntaxhighlight lang="python">def is_leap_year(year):
return not year % (4 if year % 100 ==else 0:400)</syntaxhighlight>
return year % 400 == 0
return year % 4 == 0</lang>
Asking for forgiveness instead of permission:
<langsyntaxhighlight lang="python">import datetime
 
def is_leap_year(year):
Line 2,464 ⟶ 3,255:
except ValueError:
return False
return True</langsyntaxhighlight>
 
=={{header|Q}}==
<langsyntaxhighlight lang="q">ly:{((0<>x mod 100) | 0=x mod 400) & 0=x mod 4} / Return 1b if x is a leap year; 0b otherwise</langsyntaxhighlight>
 
=={{header|Quackery}}==
{{trans|Forth}}
<langsyntaxhighlight Quackerylang="quackery"> [ dup 400 mod 0 = iff [ drop true ] done
dup 100 mod 0 = iff [ drop false ] done
4 mod 0 = ] is leap? ( n --> b )</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">isLeapYear <- function(year) {
ifelse(year%%100==0, year%%400==0, year%%4==0)
}
Line 2,482 ⟶ 3,273:
for (y in c(1900, 1994, 1996, 1997, 2000)) {
cat(y, ifelse(isLeapYear(y), "is", "isn't"), "a leap year.\n")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,493 ⟶ 3,284:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">(define (leap-year? y)
(and (zero? (modulo y 4)) (or (positive? (modulo y 100)) (zero? (modulo y 400)))))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2010.07}}
<syntaxhighlight lang="raku" perl6line>say "$year is a {Date.is-leap-year($year) ?? 'leap' !! 'common'} year."</langsyntaxhighlight>
In Rakudo 2010.07, <code>Date.is-leap-year</code> is implemented as
<syntaxhighlight lang="raku" perl6line>multi method is-leap-year($y = $!year) {
$y %% 4 and not $y %% 100 or $y %% 400
}</langsyntaxhighlight>
 
=={{header|Rapira}}==
<syntaxhighlight lang="rapira">fun is_leap_year(year)
if (year /% 100) = 0 then
return (year /% 400) = 0
fi
return (year /% 4) = 0
end</syntaxhighlight>
 
=={{header|Raven}}==
<langsyntaxhighlight Ravenlang="raven">define is_leap_year use $year
$year 100 % 0 = if
$year 400 % 0 =
$year 4 % 0 =</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight lang="rebol">leap-year?: func [
{Returns true if the specified year is a leap year; false otherwise.}
year [date! integer!]
Line 2,526 ⟶ 3,325:
div?: func [n] [zero? year // n]
to logic! any [all [div? 4 not div? 100] div? 400]
]</langsyntaxhighlight>
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">:isLeapYear? (y-f)
dup #400 mod n:zero? [ drop #-1 #0 ] [ #1 ] choose 0; drop
dup #100 mod n:zero? [ drop #0 #0 ] [ #1 ] choose 0; drop
#4 mod n:zero? ;</langsyntaxhighlight>
 
=={{header|REXX}}==
===local variables===
<langsyntaxhighlight lang="rexx">leapyear: procedure; parse arg yr
return yr//400==0 | (yr//100\==0 & yr//4==0)</langsyntaxhighlight>
 
===with short-circuit===
The REXX language doesn't support short-circuits, so here is a version that does a short-circuit.
<langsyntaxhighlight lang="rexx">leapyear: procedure; parse arg yr
if yr//4\==0 then return 0 /*Not ÷ by 4? Not a leap year.*/
return yr//400==0 | yr//100\==0</langsyntaxhighlight>
 
===no local variables===
This version doesn't need a PROCEDURE to hide local variable(s) &nbsp; [because there aren't any local variables],
<br>but it does invoke the &nbsp; '''ARG''' &nbsp; BIF multiple times.
<langsyntaxhighlight lang="rexx">leapyear: if arg(1)//4\==0 then return 0
return arg(1)//400==0 | arg(1)//100\==0</langsyntaxhighlight>
 
===handles 2 digit year===
Line 2,555 ⟶ 3,354:
<br>the current century is assumed &nbsp; (i.e., &nbsp; no ''year'' windowing).
<br><br>If a year below 100 is to be used, the year should have leading zeroes added (to make it four digits).
<langsyntaxhighlight lang="rexx">leapyear: procedure; parse arg y /*year could be: Y, YY, YYY, YYYY*/
if y//4\==0 then return 0 /*Not ÷ by 4? Not a leap year.*/
if length(y)==2 then y=left(date('S'),2)y /*adjust for a 2─digit YY year.*/
return y//100\==0 | y//400==0 /*apply 100 and 400 year rule. */</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
give year
leap = isLeapYear(year)
Line 2,572 ⟶ 3,371:
but (year % 4) = 0 return true
else return false ok
</syntaxhighlight>
</lang>
 
=={{header|RPG}}==
Line 2,578 ⟶ 3,377:
{{works with|RPGIII|}}
 
<langsyntaxhighlight RPGlang="rpg"> C*0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments+++++++
C *ENTRY PLIST
C PARM YEAR 40 input (year)
Line 2,604 ⟶ 3,403:
C*
C DONE TAG
C SETON LR</langsyntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP 100 MOD 4 400 IFTE MOD NOT
≫ '<span style="color:blue">LEAP?</span>' STO
 
2000 <span style="color:blue">LEAP?</span>
2001 <span style="color:blue">LEAP?</span>
2020 <span style="color:blue">LEAP?</span>
2100 <span style="color:blue">LEAP?</span>
{{out}}
<pre>
4: 1
3: 0
2: 1
1: 0
</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'date'
 
Date.leap?(year)</langsyntaxhighlight>
 
The leap? method is aliased as gregorian_leap? And yes, there is a julian_leap? method.
 
=={{header|Run BASIC}}==
<lang runbasic>if date$("02/29/" + mid$(date$("mm/dd/yyyy"),7,4)) then print "leap year" else print "not"</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn is_leap(year: i32) -> bool {
let factor = |x| year % x == 0;
factor(4) && (!factor(100) || factor(400))
}</langsyntaxhighlight>
 
=={{header|S-BASIC}}==
Since S-BASIC has no MOD operator or function, we have to supply one.
<lang basic>
rem - compute p mod q
function mod(p, q = integer) = integer
end = p - q * (p/q)
 
rem - return true (-1) if y is a leap year, otherwise 0
function isleapyear(y = integer) = integer
end = mod(y,4)=0 and mod(y,100)<>0 or mod(y,400)=0
 
rem - exercise the function
var y = integer
 
print "Test of century years"
for y = 1600 to 2000 step 100
if isleapyear(y) then
print y;" is a leap year"
else
print y;" is NOT a leap year"
next y
 
print "Test of current half-decade"
for y = 2015 to 2020
if isleapyear(y) then
print y; " is a leap year"
else
print y; " is NOT a leap year"
next y
 
end
</lang>
{{out}}
<pre>
Test of century years
1600 is a leap year
1700 is NOT a leap year
1800 is NOT a leap year
1900 is NOT a leap year
2000 is a leap year
Test of current half-decade
2015 is NOT a leap year
2016 is a leap year
2017 is NOT a leap year
2018 is NOT a leap year
2019 is NOT a leap year
2020 is a leap year
</pre>
 
=={{header|Scala}}==
Line 2,675 ⟶ 3,438:
By default, [http://docs.oracle.com/javase/7/docs/api/index.html?java/util/GregorianCalendar.html java.util.GregorianCalendar] switches from Julian calendar to Gregorian calendar at 15 October 1582.
 
<langsyntaxhighlight lang="scala">//use Java's calendar class
new java.util.GregorianCalendar().isLeapYear(year)</langsyntaxhighlight>
 
===JDK 8===
Using JSR-310 java.time.
<langsyntaxhighlight lang="scala">java.time.LocalDate.ofYearDay(year, 1).isLeapYear()</langsyntaxhighlight>
 
===Implementation===
Line 2,686 ⟶ 3,449:
For proleptic Gregorian calendar:
 
<langsyntaxhighlight lang="scala">def isLeapYear(year:Int)=if (year%100==0) year%400==0 else year%4==0;
 
//or use Java's calendar class
Line 2,693 ⟶ 3,456:
c.setGregorianChange(new java.util.Date(Long.MinValue))
c.isLeapYear(year)
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (leap-year? n)
(apply (lambda (a b c) (or a (and (not b) c)))
(map (lambda (m) (zero? (remainder n m)))
'(400 100 4))))</langsyntaxhighlight>
 
=={{header|sed}}==
<syntaxhighlight lang="sed">h
s/00$//
/[02468][048]$/!{
/[13579][26]$/!d
}
g
s/$/ is a leap year/</syntaxhighlight>
Test:
<pre>
$ seq 1900 2100 | sed -f leap.sed
1904 is a leap year
1908 is a leap year
1912 is a leap year
...
</pre>
 
=={{header|Seed7}}==
This function is part of the "time.s7i" library. It returns TRUE if the year is a leap year in the Gregorian calendar.
<langsyntaxhighlight lang="seed7">const func boolean: isLeapYear (in integer: year) is
return (year rem 4 = 0 and year rem 100 <> 0) or year rem 400 = 0;</langsyntaxhighlight>
Original source: [http://seed7.sourceforge.net/algorith/date.htm#isLeapYear]
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func isleap(year) {
if (year %% 100) {
return (year %% 400);
}
return (year %% 4);
}</langsyntaxhighlight>
 
or a little bit simpler:
<langsyntaxhighlight lang="ruby">func isleap(year) { year %% 100 ? (year %% 400) : (year %% 4) };</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Smalltalk has a built-in method named <tt>isLeapYear</tt>:
<langsyntaxhighlight lang="smalltalk">
Date today isLeapYear.
</syntaxhighlight>
</lang>
 
=={{header|SNOBOL4}}==
Predicate leap( ) succeeds/fails, returns nil.
<langsyntaxhighlight SNOBOL4lang="snobol4"> define('leap(yr)') :(end_leap)
leap eq(remdr(yr,400),0) :s(return)
eq(remdr(yr,100),0) :s(freturn)
Line 2,738 ⟶ 3,518:
yr = '1900'; eval(test)
yr = '2000'; eval(test)
end</langsyntaxhighlight>
{{out}}
<pre>0: 1066
Line 2,744 ⟶ 3,524:
0: 1900
1: 2000</pre>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">fun isLeapYear y =
y mod (if y mod 100 = 0 then 400 else 4) = 0</syntaxhighlight>
 
=={{header|Stata}}==
Line 2,749 ⟶ 3,533:
Given a dataset with a "year" variable, generate a variable "leap" which is 1 for a leap year, 0 otherwise.
 
<langsyntaxhighlight lang="stata">gen leap = mod(year,400)==0 | mod(year,4)==0 & mod(year,100)!=0</langsyntaxhighlight>
 
See also the article '''[https://www.stata.com/support/faqs/data-management/leap-year-indicators/ How do I identify leap years in Stata?]''' by Nicholas J. Cox in Stata FAQ.
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">func isLeapYear(year: Int) -> Bool {
return year.isMultiple(year %of: 100 == 0) ? year.isMultiple(year %of: 400 == 0) : year.isMultiple(year %of: 4 == 0)
}
 
[1900, 1994, 1996, 1997, 2000].forEach { year in
print(isLeapYear(2000))
print("\(year): \(isLeapYear(2011year: year) ? "YES" : "NO")")</lang>
} </syntaxhighlight>
{{out}}
<pre>true1900: NO
1994: NO
false
1996: YES
1997: NO
2000: YES
</pre>
 
=={{header|Tcl}}==
The "classic" modulo comparison:
<langsyntaxhighlight lang="tcl">proc isleap1 {year} {
return [expr {($year % 4 == 0) && (($year % 100 != 0) || ($year % 400 == 0))}]
}
Line 2,773 ⟶ 3,561:
isleap1 1989 ;# => 0
isleap1 1900 ;# => 0
isleap1 2000 ;# => 1</langsyntaxhighlight>
Does Feb 29 exist in the given year? If not a leap year, the clock command will return "03-01". (This code will switch to the Julian calendar for years before 1582.)
<langsyntaxhighlight lang="tcl">proc isleap2 year {
return [expr {[clock format [clock scan "$year-02-29" -format "%Y-%m-%d"] -format "%m-%d"] eq "02-29"}]
}
Line 2,781 ⟶ 3,569:
isleap2 1989 ;# => 0
isleap2 1900 ;# => 0
isleap2 2000 ;# => 1</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
LOOP year="1900'1994'1996'1997'2000",txt=""
SET dayoftheweek=DATE(number,29,2,year,number)
IF (dayoftheweek==0) SET txt="not "
PRINT year," is ",txt,"a leap year"
ENDLOOP</langsyntaxhighlight>
{{out}}
<pre>
Line 2,799 ⟶ 3,587:
</pre>
 
=={{header|uBasic/4tHUNIX Shell}}==
POSIX compatible:
{{trans|BBC BASIC}}
<syntaxhighlight lang="sh">is_leap() {
<lang>DO
return $((${1%00} & 3))
INPUT "Enter a year: "; y
}</syntaxhighlight>
IF FUNC(_FNleap(y)) THEN
PRINT y; " is a leap year"
ELSE
PRINT y; " is not a leap year"
ENDIF
LOOP
END
 
_FNleap Param (1)
RETURN ((a@ % 4 = 0) * ((a@ % 400 = 0) + (a@ % 100 # 0)))</lang>
 
=={{header|UNIX Shell}}==
Original Bourne:
<langsyntaxhighlight lang="sh">leap() {
if expr $1 % 4 >/dev/null; then return 1; fi
if expr $1 % 100 >/dev/null; then return 0; fi
if expr $1 % 400 >/dev/null; then return 1; fi
return 0;
}</langsyntaxhighlight>
 
Using GNU date(1):
<langsyntaxhighlight lang="sh">leap() {
date -d "$1-02-29" >/dev/null 2>&1;
}</langsyntaxhighlight>
 
Defining a bash function <tt>is_leap</tt> which accepts a YEAR argument (defaulting to zero), and uses no IO redirection, nor any extra processes.
<syntaxhighlight lang="bash">is_leap() (( year=${1-0}, year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 )))
<lang sh>is_leap() {
</syntaxhighlight>
local year=$(( 10#${1:?'Missing year'} ))
(( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) )) && return 0
return 1
}</lang>
 
Using the cal command: ''(note that this invokes two processes with IO piped between them and is relatively heavyweight compared to the above shell functions: leap and is_leap)''
Line 2,841 ⟶ 3,616:
<!-- actually, it *is* correct for dates from 1752 and after. Since the Gregorian calendar didn't exist prior to that date,
it doesn't make sense to hold this task accountable for years prior -->
<langsyntaxhighlight lang="sh">leap() {
cal 02 $1 | grep -q 29
}
</syntaxhighlight>
</lang>
 
=={{header|Ursa}}==
This program takes a year as a command line argument.
<langsyntaxhighlight lang="ursa">decl int year
set year (int args<1>)
if (= (mod year 4) 0)
Line 2,858 ⟶ 3,633:
else
out year " is not a leap year" endl console
end if</langsyntaxhighlight>
Output in Bash:
<pre>$ ursa leapyear.u 1900
Line 2,866 ⟶ 3,641:
 
=={{header|Vala}}==
<langsyntaxhighlight Valalang="vala">void main() {
DateYear[] years = { 1900, 1994, 1996, 1997, 2000 };
foreach ( DateYear year in years ) {
string status = year.is_leap_year() ? "" : "not ";
print print(@"%d$year is %sa$(status)a leap year.\n", (int) year, status);
}
}</langsyntaxhighlight>
{{out}}
 
=={{header|VBA}}==
<lang vb>Public Function Leap_year(year As Integer) As Boolean
Leap_year = (Month(DateSerial(year, 2, 29)) = 2)
End Function</lang>
 
=={{header|VBScript}}==
<lang vb>
Function IsLeapYear(yr)
IsLeapYear = False
If yr Mod 4 = 0 And (yr Mod 400 = 0 Or yr Mod 100 <> 0) Then
IsLeapYear = True
End If
End Function
 
'Testing the function.
arr_yr = Array(1900,1972,1997,2000,2001,2004)
 
For Each yr In arr_yr
If IsLeapYear(yr) Then
WScript.StdOut.WriteLine yr & " is leap year."
Else
WScript.StdOut.WriteLine yr & " is NOT leap year."
End If
Next
</lang>
 
{{Out}}
<pre>
1900 is NOTnot a leap year.
19721994 is not a leap year.
19971996 is NOTa leap year.
20001997 is not a leap year.
20012000 is NOTa leap year.
2004 is leap year.
</pre>
 
=={{header|Vedit macro language}}==
<langsyntaxhighlight lang="vedit">while (#1 = Get_Num("Year: ")) {
#2 = (#1 % 4 == 0) && ((#1 % 100 != 0) || (#1 % 400 == 0))
if (#2) {
Line 2,918 ⟶ 3,665:
Message(" is not leap year\n")
}
}</langsyntaxhighlight>
 
The following version requires Vedit 6.10 or later:
<langsyntaxhighlight lang="vedit">while (#1 = Get_Num("Year: ")) {
if (Is_Leap_Year(#1)) {
Message(" is leap year\n")
Line 2,927 ⟶ 3,674:
Message(" is not leap year\n")
}
}</langsyntaxhighlight>
 
=={{header|VisualV Basic(Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn is_leap(year int) bool {
{{works with|Visual Basic|VB6 Standard}}
return year %400 ==0 || (year%4 ==0 && year%100!=0)
<lang vb>
}
Public Function IsLeapYear1(ByVal theYear As Integer) As Boolean
'this function utilizes documented behaviour of the built-in DateSerial function
fn main() {
IsLeapYear1 = (VBA.Day(VBA.DateSerial(theYear, 2, 29)) = 29)
for y in 1950..2012 {
End Function
if is_leap(y) {
 
println(y)
Public Function IsLeapYear2(ByVal theYear As Integer) As Boolean
}
'this function uses the well-known formula
}
IsLeapYear2 = IIf(theYear Mod 100 = 0, theYear Mod 400 = 0, theYear Mod 4 = 0)
}
End Function
</syntaxhighlight>
</lang>
Returns:
Testing:
<pre>1952
<lang vb>
1956
Sub Main()
1960
'testing the above functions
1964
Dim i As Integer
1968
For i = 1750 To 2150
1972
Debug.Assert IsLeapYear1(i) Eqv IsLeapYear2(i)
1976
Next i
1980
End Sub
1984
</lang>
1988
 
1992
=={{header|Visual Basic .NET}}==
1996
{{trans|C#}}
2000
<lang vbnet>Module Module1
2004
 
2008
Sub Main()
</pre>
For Each y In {1900, 1994, 1996, Date.Now.Year}
Console.WriteLine("{0} is {1}a leap year.", y, If(Date.IsLeapYear(y), String.Empty, "not "))
Next
End Sub
 
End Module</lang>
{{out}}
<pre>1900 is not a leap year.
1994 is not a leap year.
1996 is a leap year.
2019 is not a leap year.</pre>
 
=={{header|WDTE}}==
<langsyntaxhighlight WDTElang="wdte">let str => import 'strings';
 
let multiple of n => == (% n of) 0;
Line 2,980 ⟶ 3,717:
multiple 4 => '';
default => ' not';
}) -- io.writeln io.stdout;</langsyntaxhighlight>
 
=={{header|WebAssembly}}==
First, with syntactic sugar that allows us to put opcode arguments after the opcode itself:
<syntaxhighlight lang="webassembly">(module
;; function isLeapYear: returns 1 if its argument (e.g. 2004) is a leap year, 0 otherwise.
;; Returns year%4==0 and (year%100!=0 or year%400==0)
(func $isLeapYear (param $year i32) (result i32)
(i32.and
(i32.eqz (i32.rem_u (get_local $year) (i32.const 4))) ;; year%4 == 0
(i32.or
(i32.ne (i32.rem_u (get_local $year) (i32.const 100)) (i32.const 0)) ;; year%100 != 0
(i32.eqz (i32.rem_u (get_local $year) (i32.const 400))) ;; yaer%400 == 0
)
)
)
(export "isLeapYear" (func $isLeapYear))
)</syntaxhighlight>
 
And then the same code, without the syntactic sugar:
<syntaxhighlight lang="webassembly">(module
;; function isLeapYear: returns 1 if its argument (e.g. 2004) is a leap year, 0 otherwise.
;; Returns year%4==0 and (year%100!=0 or year%400==0)
(func $isLeapYear (param $year i32) (result i32)
get_local $year
i32.const 4
i32.rem_u
i32.eqz ;; year % 4 == 0
get_local $year
i32.const 100
i32.rem_u
i32.const 0
i32.ne ;; year % 100 != 0
get_local $year
i32.const 400
i32.rem_u
i32.eqz ;; year % 400 == 0
i32.or
i32.and
)
(export "isLeapYear" (func $isLeapYear))
)</syntaxhighlight>
 
=={{header|Wortel}}==
<langsyntaxhighlight lang="wortel">@let {
isLeapYear !?{\~%%1H \~%%4H \~%%4}
!-isLeapYear @range[1900 2000]
}</langsyntaxhighlight>
Returns:
<pre>[1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000]</pre>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var isLeapYear = Fn.new { |y|
return ((y % 4 == 0) && (y % 100!= 0)) || (y % 400 == 0)
}
Line 3,003 ⟶ 3,781:
if (c % 15 == 0) System.print()
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,014 ⟶ 3,792:
=={{header|X86 Assembly}}==
Using FASM syntax. Leaf function fits nicely into your program.
<langsyntaxhighlight lang="asm"> align 16
; Input year as signed dword in EAX
IsLeapYear:
Line 3,030 ⟶ 3,808:
.100:
test eax,11b
retn ; 1% : ZF=?, leap year if EAX%400=0</langsyntaxhighlight>
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="xlisp">(DEFUN LEAP-YEARP (YEAR)
(AND (= (MOD YEAR 4) 0) (OR (/= (MOD YEAR 100) 0) (= (MOD YEAR 400) 0))))
 
; Test the function
(DISPLAY (MAPCAR LEAP-YEARP '(1600 1640 1800 1928 1979 1990 2000 2004 2005 2016)))</langsyntaxhighlight>
{{out}}
<pre>(#T #T () #T () () #T #T () #T)</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func LeapYear(Y); \Return 'true' if Y is a leap year
int Y;
[if rem(Y/100)=0 then return rem(Y/400)=0;
return rem(Y/4)=0;
];</langsyntaxhighlight>
 
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
!yamlscript/v0
 
defn main(year):
say: "$year is $when-not(leap-year(year) 'not ')a leap year."
 
# Either one works:
 
defn leap-year(year):
((year % 4) == 0) && (((year % 100) > 0) || ((year % 100) == 0))
 
defn leap-year(year):
and:
zero?: (year % 4)
or:
pos?: (year % 100)
zero?: (year % 400)
</syntaxhighlight>
 
=={{header|Yorick}}==
This solution is vectorized and can be applied to scalar or array input.
<langsyntaxhighlight lang="yorick">func is_leap(y) {
return ((y % 4 == 0) & (y % 100 != 0)) | (y % 400 == 0);
}</langsyntaxhighlight>
Interactive example usage:
<pre>> is_leap(1988)
Line 3,058 ⟶ 3,856:
> is_leap([1988,1989,1900,2000])
[1,0,0,1]</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="Zig">pub fn isLeapYear(year: anytype) bool {
const inttype = @TypeOf(year);
if (@typeInfo(inttype) != .Int) {
@compileError("non-integer type used on leap year: " ++ @typeName(inttype));
}
return (if (@mod(year, @as(inttype, 100)) == 0)
@mod(year, @as(inttype, 400)) == 0
else
@mod(year, @as(inttype, 4)) == 0);
}</syntaxhighlight>
 
Alternative (inspired by the C solution):
 
<syntaxhighlight lang="Zig">
/// The type that holds the current year, i.e. 2016
pub const Year = u16;
 
/// Returns true for years with 366 days
/// and false for years with 365 days.
pub fn isLeapYear(year: Year) bool {
// In the western Gregorian Calendar leap a year is
// a multiple of 4, excluding multiples of 100, and
// adding multiples of 400. In code:
//
// if (@mod(year, 4) != 0)
// return false;
// if (@mod(year, 100) != 0)
// return true;
// return (0 == @mod(year, 400));
 
// The following is equivalent to the above
// but uses bitwise operations when testing
// for divisibility, masking with 3 as test
// for multiples of 4 and with 15 as a test
// for multiples of 16. Multiples of 16 and
// 100 are, conveniently, multiples of 400.
const mask: Year = switch (year % 100) {
0 => 0b1111,
else => 0b11,
};
return 0 == year & mask;
}
 
test "isLeapYear" {
try testing.expectEqual(false, isLeapYear(2095));
try testing.expectEqual(true, isLeapYear(2096));
try testing.expectEqual(false, isLeapYear(2100));
try testing.expectEqual(true, isLeapYear(2400));
}
</syntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">Time.Date.isLeapYear(1988) //-->True
T(1988,1989,1900,2000).apply(Time.Date.isLeapYear)
//-->L(True,False,False,True)</langsyntaxhighlight>
 
{{omit from|GUISS|Only the operator can read the calendar}}
15

edits