Leap year: Difference between revisions

5,951 bytes added ,  2 months ago
m (syntax highlighting fixup automation)
 
(34 intermediate revisions by 13 users not shown)
Line 586:
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}}
Line 609 ⟶ 610:
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}}===
Line 631 ⟶ 658:
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}}===
Line 639 ⟶ 693:
===={{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
Line 668 ⟶ 921:
180 DEF LEAPY(Y)=MOD(Y,4)=0 AND MOD(Y,100) OR MOD(Y,400)=0</syntaxhighlight>
 
==={{header|Sinclair ZX81Liberty BASIC}}===
==== Simple method ====
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="basiclb">5000 LET L=Y/4=INTif leap(Y/4) AND (Y/100<>INT (Y/100) OR Y/400=INT (Y/400)1996)then
print "leap"
5010 RETURN</syntaxhighlight>
else
An example showing how to call it:
print "ordinary"
<syntaxhighlight lang="basic">10 INPUT Y
end if
20 GOSUB 5000
wait
30 PRINT Y;" IS ";
40 IF NOT L THEN PRINT "NOT ";
50 PRINT "A LEAP YEAR."
60 STOP</syntaxhighlight>
 
function leap(n)
==={{Header|Tiny BASIC}}===
leap=date$("2/29/";n)
<syntaxhighlight lang="tiny basic">REM Rosetta Code problem: https://rosettacode.org/wiki/Leap_year
end function</syntaxhighlight>
REM by Jjuanhdez, 06/2022
 
==== Calculated method ====
LET Y = 1750
<syntaxhighlight lang="lb"> year = 1908
10 IF Y = 2021 THEN GOTO 20
GOSUBselect 100case
IF L = 1 THENcase PRINTyear mod 400 = Y0
LET Y = Y + leapYear = 1
case year mod 4 = 0 and year mod 100 <> 0
GOTO 10
leapYear = 1
20 END
case else
leapYear = 0
100 LET L = 0
end select
IF (Y - (Y / 4) * 4) <> 0 THEN RETURN
if leapYear = 1 then
IF (Y - (Y / 100) * 100) = 0 THEN GOTO 110
print year;" is a leap year."
LET L = 1
else
110 IF (Y - (Y / 400) * 400) <> 0 THEN GOTO 120
print year;" is not a leap year."
LET L = 1
120 RETURN end if</syntaxhighlight>
 
==={{header|ZX Spectrum BasicNS-HUBASIC}}===
<syntaxhighlight lang="zxbasicns-hubasic">10 DEFINPUT FN"ENTER l(y)=y/4=INTA (y/4)NUMBER, AND (y/100<>INTI'LL (y/100)DETECT IF IT'S A LEAP YEAR OR y/400=INTNOT. (y/400))",A
20 IF A-(A/100)*100=0 AND A-(A/400)*400<>0 THEN RESULT$="NOT "
</syntaxhighlight>
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}}===
Line 755 ⟶ 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
<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
 
==={{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</syntaxhighlight>
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}}==
Line 829 ⟶ 1,277:
Press any key to continue . . .</pre>
 
 
=={{header|bc}}==
<syntaxhighlight lang="bc">define l(y) {
if (y % 100 == 0) y /= 100
if (y % 4 == 0) return(1)
return(0)
}</syntaxhighlight>
 
=={{header|BCPL}}==
Line 874 ⟶ 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}}==
Line 903 ⟶ 1,367:
<syntaxhighlight 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;
}</syntaxhighlight>
{{out}}
Line 923 ⟶ 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>
 
Line 974 ⟶ 1,447:
 
=={{header|Clojure}}==
A simple approach:
<syntaxhighlight lang="clojure">(defn leap-year? [y]
(and (zero? (mod y 4)) (or (pos? (mod y 100)) (zero? (mod y 400)))))</syntaxhighlight>
(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}}==
Line 1,320 ⟶ 1,805:
 
<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}}==
Line 1,489 ⟶ 1,985:
{{out}}
<pre> F T F T </pre>
 
=={{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|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Leap_year}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Leap year 01.png]]
In '''[https://formulae.org/?example=Leap_year this]''' page you can see the program(s) related to this task and their results.
 
In a more concise way:
=={{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."
 
[[File:Fōrmulæ - Leap year 02.png]]
Try dDate = Date(siYear, 02, 29)
If Error Then sMessage = " is not a leap year."
 
[[File:Fōrmulæ - Leap year 03.png]]
Message(siYear & sMessage)
 
[[File:Fōrmulæ - Leap year 04.png]]
End</syntaxhighlight>
 
Output:
<pre>
2016 is a leap year.
</pre>
 
=={{header|GAP}}==
Line 1,745 ⟶ 2,076:
2008
2012</pre>
 
=={{header|GW-BASIC}}==
{{works with|PC-BASIC|any}}
<syntaxhighlight 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
</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|Harbour}}==
Line 1,889 ⟶ 2,196:
<syntaxhighlight lang="javascript">// Month values start at 0, so 1 is for February
var isLeapYear = function (year) { return new Date(year, 1, 29).getDate() === 29; };</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE leapyear == dup 100 div null rotate choice 4 rem null.</syntaxhighlight>
 
=={{header|jq}}==
Line 1,915 ⟶ 2,225:
 
=={{header|K}}==
=== K3 ===
<syntaxhighlight 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
a@&leapyear'a:1900,1994,1996,1997,2000
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.
<syntaxhighlight lang="koka"> import std/time
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}}==
<syntaxhighlight lang="kotlin">fun isLeapYear(year: Int) = year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)</syntaxhighlight>
Line 1,944 ⟶ 2,280:
true</pre>
 
=={{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|Lingo}}==
Line 2,464 ⟶ 2,772:
<pre>true
true</pre>
 
=={{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|Oberon-2}}==
Line 2,521 ⟶ 2,824:
=={{header|OCaml}}==
<syntaxhighlight 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</syntaxhighlight>
Using Unix Time functions:
<syntaxhighlight lang="ocaml">let is_leap_year ~year =
Line 2,644 ⟶ 2,945:
Or more concisely:
 
<syntaxhighlight lang="perl">sub isleap { !(not $_[0] % 100) ? !($_[0] % 400)100 :? !($_[0]4 %: 4400) }</syntaxhighlight>
 
Alternatively, using functions/methods from CPAN modules:
Line 2,717 ⟶ 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}}==
Line 2,846 ⟶ 3,240:
L = [1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028].
</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|Python}}==
Line 2,861 ⟶ 3,246:
or
<syntaxhighlight 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</syntaxhighlight>
Asking for forgiveness instead of permission:
<syntaxhighlight lang="python">import datetime
Line 3,022 ⟶ 3,405:
C SETON LR</syntaxhighlight>
 
=={{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}}==
<syntaxhighlight lang="ruby">require 'date'
Line 3,028 ⟶ 3,427:
 
The leap? method is aliased as gregorian_leap? And yes, there is a julian_leap? method.
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">if date$("02/29/" + mid$(date$("mm/dd/yyyy"),7,4)) then print "leap year" else print "not"</syntaxhighlight>
 
=={{header|Rust}}==
Line 3,037 ⟶ 3,433:
factor(4) && (!factor(100) || factor(400))
}</syntaxhighlight>
 
=={{header|S-BASIC}}==
Since S-BASIC has no MOD operator or function, we have to supply one.
<syntaxhighlight 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
</syntaxhighlight>
{{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 3,116 ⟶ 3,463:
(map (lambda (m) (zero? (remainder n m)))
'(400 100 4))))</syntaxhighlight>
 
=={{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}}==
Line 3,223 ⟶ 3,587:
</pre>
 
=={{header|uBasic/4tHUNIX Shell}}==
POSIX compatible:
{{trans|BBC BASIC}}
<syntaxhighlight lang="textsh">DOis_leap() {
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)))</syntaxhighlight>
 
=={{header|UNIX Shell}}==
Original Bourne:
<syntaxhighlight lang="sh">leap() {
Line 3,252 ⟶ 3,606:
}</syntaxhighlight>
 
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="shbash">is_leap() (( year=${1-0}, year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 )))
</syntaxhighlight>
local year=$(( 10#${1:?'Missing year'} ))
(( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) )) && return 0
return 1
}</syntaxhighlight>
 
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 3,304 ⟶ 3,655:
1997 is not a leap year.
2000 is a leap year.
</pre>
 
=={{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>
 
Line 3,361 ⟶ 3,676:
}</syntaxhighlight>
 
=={{header|VisualV Basic(Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn is_leap(year int) bool {
{{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|Vlang}}==
<syntaxhighlight lang="vlang">fn is_leap(year int) bool {
return year %400 ==0 || (year%4 ==0 && year%100!=0)
}
Line 3,495 ⟶ 3,769:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var isLeapYear = Fn.new { |y|
return ((y % 4 == 0) && (y % 100!= 0)) || (y % 400 == 0)
}
Line 3,552 ⟶ 3,826:
];</syntaxhighlight>
 
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
!yamlscript/v0
 
defn main(year):
=={{header|Yabasic}}==
say: "$year is $when-not(leap-year(year) 'not ')a leap year."
<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
 
# Either one works:
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
 
defn leap-year(year):
print
((year % 4) == 0) && (((year % 100) > 0) || ((year % 100) == 0))
 
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>
 
defn leap-year(year):
and:
zero?: (year % 4)
or:
pos?: (year % 100)
zero?: (year % 400)
</syntaxhighlight>
 
=={{header|Yorick}}==
Line 3,585 ⟶ 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}}==
15

edits