Leap year: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 12:
{{trans|Python}}
<
I year % 100 == 0
R year % 400 == 0
R year % 4 == 0</
=={{header|360 Assembly}}==
Line 27:
When R15 = zero, the year is a leap year.
Otherwise it is not.
<
LPCK CSECT
USING LPCK,15
Line 46:
BR 14
END
</
Sample invocation from a COBOL program:
Line 72:
=={{header|68000 Assembly}}==
<
move.l #2018,d0
bsr leap_year
Line 116:
moveq.l #0,d1
rts
</syntaxhighlight>
=={{header|8080 Assembly}}==
<
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 180:
jmp 5
no: db 'NOT '
yes: db 'LEAP YEAR.$'</
=={{header|Action!}}==
<
IF year MOD 100=0 THEN
IF year MOD 400=0 THEN
Line 213:
FI
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Leap_year.png Screenshot from Atari 8-bit computer]
Line 227:
=={{header|ActionScript}}==
<
if (year % 100 == 0) {
return (year % 400 == 0);
}
return (year % 4 == 0);
}</
=={{header|Ada}}==
<
-- Adjust the type of Year if you use a different one.
function Is_Leap_Year (Year : Integer) return Boolean is
Line 269:
-- To improve speed a bit more, use with
pragma Inline (Is_Leap_Year);</
=={{header|ALGOL 60}}==
{{works with|A60}}
<
integer year;
Line 286:
if isLeapYear(year) then outstring(1,"True\n") else outstring(1, "False\n")
end for year
end </
{{out}}
<pre>
Line 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}}
<
PROC year days = (YEAR year)DAY: # Ignore 1752 CE for the moment #
Line 326:
printf(($g(0)" is "b("","not ")"a leap year."l$, year, is leap year(year)))
OD
)</
{{out}}
<pre>
Line 337:
=={{header|ALGOL W}}==
<
% returns true if year is a leap year, false otherwise %
% assumes year is in the Gregorian Calendar %
Line 352:
)
end for_year
end.</
=={{header|ALGOL-M}}==
<
BEGIN
Line 399:
END
</syntaxhighlight>
{{out}}
<pre>
Line 424:
=={{header|APL}}==
Returns 1 if leap year, 0 otherwise:
<
∇ z←Leap year
Z←(0=4|year)∧(0=400|year)∨~0=100|year
∇
</syntaxhighlight>
A much neater version of the above relies on the fact that every rule is an exception the the previous one:
<
∇ 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:
<
Leap←0≠.=400 100 4∘.|⊢
</syntaxhighlight>
Dyalog APL version 18.0 added a built-in date-time function:
<
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:
<
Leap 1899 1900 1901 1902 1903 1904 1905 1999 2000 2001 2002 2003 2004
</syntaxhighlight>
{{out}}
<
0 0 0 0 0 1 0 0 1 0 0 0 1
</syntaxhighlight>
=={{header|AppleScript}}==
<
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)</
=={{header|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>
<b>Output:</b>
<
(map [leap? _] '(1900 1904 2000 2019 2020 2100))
;; => '( 1904 2000 2020 )
</syntaxhighlight>
=={{header|Arturo}}==
<
1600 1660 1724 1788 1848 1912 1972
2032 2092 2156 2220 2280 2344 2348
Line 481:
]
print select years => leap?</
{{out}}
Line 488:
=={{header|AutoHotkey}}==
<
{
if (Mod(year, 100) = 0)
Line 495:
}
MsgBox, % leapyear(1604)</
{{out}}
<pre>Returns 1 if year is a leap year</pre>
or
<
{
return !Mod(Year, 4) && Mod(Year, 100) || !Mod(Year, 400)
}
MsgBox % "The year 1604 was " (IsLeapYear(1604) ? "" : "not ") "a leap year"</
{{out}}
<pre>The year 1600 was a leap year
Line 511:
=={{header|AutoIt}}==
<
$Year = 2012
$sNot = " not"
Line 523:
; == But it exists the standard UDF "Date.au3" with this function: "_IsLeapYear($Year)"
</syntaxhighlight>
{{out}}
<pre>
Line 531:
=={{header|AWK}}==
<
{
if ( year % 100 == 0 )
Line 537:
else
return ( year % 4 == 0 )
}</
=={{header|Bash}}==
<syntaxhighlight lang="bash">
#!/bin/bash
Line 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>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
A one-liner combination from the [[#Commodore_BASIC|Commodore BASIC]] and [[#GW_BASIC|GW-BASIC]] solutions.
<
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
function leapyear(year_)
if (year_ mod 4) <> 0 then return FALSE
Line 607:
if (year_ mod 4) = 3 then print ""
next year_
end</
==={{header|BBC BASIC}}===
<
INPUT "Enter a year: " year%
IF FNleap(year%) THEN
Line 622:
DEF FNleap(yr%)
= (yr% MOD 4 = 0) AND ((yr% MOD 400 = 0) OR (yr% MOD 100 <> 0))</
Much quicker without full evaluation:
<
IF yr% MOD 4 THEN =FALSE
IF yr% MOD 400 ELSE =TRUE
IF yr% MOD 100 ELSE =FALSE
=TRUE</
==={{header|Commodore BASIC}}===
An old-timey solution:
<
Or, using Simons' BASIC's MOD function:
===={{header|Simons' BASIC}}====
<
==={{header|GW-BASIC}}===
Prints all the leap years from 1750 to 2021. Note the correct behaviour of 1800, 1900, and 2000.
<
20 GOSUB 1000
30 IF L = 1 THEN PRINT Y;" ";
Line 651:
1020 IF Y MOD 100 = 0 AND Y MOD 400 <> 0 THEN RETURN
1030 L = 1
1040 RETURN</
{{out}}<pre>
1752 1756 1760 1764 1768 1772 1776 1780 1784 1788 1792 1796 1804 1808 1812 1816 1820 1824 1828
Line 658:
==={{header|IS-BASIC}}===
<
110 FOR I=1990 TO 2020
120 IF LEAPY(I) THEN
Line 666:
160 END IF
170 NEXT
180 DEF LEAPY(Y)=MOD(Y,4)=0 AND MOD(Y,100) OR MOD(Y,400)=0</
==={{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.
<
5010 RETURN</
An example showing how to call it:
<
20 GOSUB 5000
30 PRINT Y;" IS ";
40 IF NOT L THEN PRINT "NOT ";
50 PRINT "A LEAP YEAR."
60 STOP</
==={{Header|Tiny BASIC}}===
<
REM by Jjuanhdez, 06/2022
Line 698:
110 IF (Y - (Y / 400) * 400) <> 0 THEN GOTO 120
LET L = 1
120 RETURN</
==={{header|ZX Spectrum Basic}}===
<
</syntaxhighlight>
==={{header|QBasic}}===
Note that the <code>year%</code> function is not needed for most modern BASICs.
<
DECLARE FUNCTION isLeapYear% (yr AS INTEGER)
DECLARE FUNCTION year% (date AS STRING)
Line 730:
FUNCTION year% (date AS STRING)
year% = VAL(RIGHT$(date, 4))
END FUNCTION</
==={{header|QL SuperBASIC}}===
<
AUTO
REM Is% a non-proleptic Gregorian year y$<=9999 leap (0) 0R ordinary (1)?
Line 743:
END DEF Is%
ctrl+space
</syntaxhighlight>
using only power-of-2 divisions. N.B. the inverted logic brings home the BaCon code's flaw
{{output}}
Line 757:
==={{header|BaCon}}===
From the Ada shortcut calculation
<
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)
Line 768:
WEND
DATA 1600, 1700, 1800, 1900, 1901, 1996, 2000, 2001, 2004, 0</
{{out}}
Line 783:
=={{header|Batch File}}==
<
::The Main Thing...
Line 811:
echo %year% is NOT a leap year.
goto :EOF
::/The Function...</
{{out}}
<pre>1900 is NOT a leap year.
Line 831:
=={{header|BCPL}}==
<
let leap(year) = year rem 400 = 0 | (year rem 4 = 0 & year rem 100 ~= 0)
Line 841:
writef("%N %S a leap year.*N",
years!i, leap(years!i) -> "is", "is not")
$)</
{{out}}
<pre>1899 is not a leap year.
Line 862:
{{trans|C}}
<
v*%"d"\!%4::,,"is".:<|
>\45*:*%!+#v_ "ton"vv<
v"ear."+550<,,,,*84<$#
>"y pael a ">:#,_$:#@^</
{{out}}
Line 876:
=={{header|Bracmat}}==
<
=
. mod$(!arg.100):0
Line 890:
)
)
& ;</
{{out}}
<pre>1600 is a leap year
Line 901:
=={{header|C}}==
<
int is_leap_year(int year)
Line 915:
printf("%d is %sa leap year.\n", year, (is_leap_year(year) == 1 ? "" : "not "));
}
}</
{{out}}
<pre>
Line 926:
=={{header|C sharp|C#}}==
<
class Program
Line 939:
}
}
}</
{{out}}
<pre>1900 is not a leap year.
Line 949:
Uses C++11. Compile with
g++ -std=c++11 leap_year.cpp
<
bool is_leap_year(int year) {
Line 959:
std::cout << year << (is_leap_year(year) ? " is" : " is not") << " a leap year.\n";
}
}</
{{out}}
<pre>
Line 970:
=={{header|Clipper}}==
<
Return Iif( nYear%100 == 0, (nYear%400 == 0), (nYear%4 == 0) )</
=={{header|Clojure}}==
<
(and (zero? (mod y 4)) (or (pos? (mod y 100)) (zero? (mod y 400)))))</
=={{header|CLU}}==
<
return(year//400 =0 cor (year//4 = 0 cand year//100 ~= 0))
end is_leap_year
Line 993:
stream$putl(po, "a leap year.")
end
end start_up</
{{out}}
<pre>1899 is not a leap year.
Line 1,012:
=={{header|COBOL}}==
<
PROGRAM-ID. leap-year.
Line 1,040:
GOBACK
.</
Using Date Intrinsic Functions
<syntaxhighlight lang="cobol">
program-id. leap-yr.
*> Given a year, where 1601 <= year <= 9999
Line 1,077:
.
end program leap-yr.
</syntaxhighlight>
{{out}}
<pre>
Line 1,095:
=={{header|Common Lisp}}==
<
(destructuring-bind (fh h f)
(mapcar #'(lambda (n) (zerop (mod year n))) '(400 100 4))
(or fh (and (not h) f))))</
=={{header|Component Pascal}}==
BlackBox Component Builder
<
MODULE LeapYear;
IMPORT StdLog, Strings, Args;
Line 1,132:
END Do;
END LeapYear.
</syntaxhighlight>
Execute: ^Q LeapYear.Do 2000 2004 2013~<br/>
{{out}}
Line 1,143:
=={{header|Crystal}}==
<
p Time.leap_year?(2021)
p Time.leap_year?(2022)</
<pre>
Line 1,154:
=={{header|D}}==
<
bool leapYear(in uint y) pure nothrow {
Line 1,166:
1973, 2100, 2107, 2200, 2203, 2289];
assert(filter!leapYear(bad ~ good).equal(good));
}</
Using the datetime library:
<
void main() {
Line 1,178:
assert(DateTime(2000, 1, 1).isLeapYear);
}
</syntaxhighlight>
=={{header|Dart}}==
<
bool leapYear(num year) {
return (year % 400 == 0) || (( year % 100 != 0) && (year % 4 == 0));
Line 1,190:
// Source: https://api.flutter.dev/flutter/quiver.time/isLeapYear.html
}
}</
=={{header|Dc}}==
Directly taken from Wikipedia.
{{works with|GNU dc}}
<
[1q]s1
Line 1,218:
1989 lTx
1900 lTx
2000 lTx</
{{out}}
<pre>
Line 1,229:
=={{header|Delphi}}/{{header|Pascal}}==
Delphi has standard function IsLeapYear in SysUtils unit.
<
{$APPTYPE CONSOLE}
Line 1,247:
Writeln(Year, ' is not a Leap year');
Readln;
end.</
=={{header|Draco}}==
<
year%400=0 or (year%4=0 and year%100/=0)
corp
Line 1,264:
"a leap year.")
od
corp</
{{out}}
<pre>1899 is not a leap year.
Line 1,283:
=={{header|DWScript}}==
<
begin
Result:= (y mod 4 = 0)
Line 1,303:
PrintLn('Checking non-leap years');
for i in bad do
if IsLeapYear(i) then PrintLn(i);</
=={{header|Dyalect}}==
<
if y % 100 == 0 {
y % 400 == 0
Line 1,315:
}
print(isLeap(1984))</
{{out}}
Line 1,322:
=={{header|Ela}}==
<
| else = y % 4 == 0</
=={{header|Elixir}}==
<
IO.inspect for y <- 2000..2020, leap_year?.(y), do: y</
{{out}}
Line 1,336:
=={{header|Emacs Lisp}}==
{{trans|Scheme}}
<
(apply (lambda (a b c) (or a (and (not b) c)))
(mapcar (lambda (n) (zerop (mod year n)))
'(400 100 4))))</
=={{header|Erlang}}==
<
-module(gregorian).
-export([leap/1]).
leap( Year ) -> calendar:is_leap_year( Year ).
</syntaxhighlight>
=={{header|ERRE}}==
<
FUNCTION LEAP(YR%)
Line 1,366:
END IF
END LOOP
END PROGRAM</
=={{header|Euphoria}}==
<
return remainder(year,4)=0 and remainder(year,100)!=0 or remainder(year,400)=0
end function</
=={{header|Excel}}==
Take two cells, say A1 and B1, in B1 type in :
<syntaxhighlight lang="excel">
=IF(OR(NOT(MOD(A1,400)),AND(NOT(MOD(A1,4)),MOD(A1,100))),"Leap Year","Not a Leap Year")
</syntaxhighlight>
{{out}}
Line 1,398:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(y,
OR(
Line 1,407:
)
)
)</
{{Out}}
Line 1,445:
=={{header|F_Sharp|F#}}==
<
assert isLeapYear 1996
assert isLeapYear 2000
assert not (isLeapYear 2001)
assert not (isLeapYear 1900)</
=={{header|Factor}}==
Call ''leap-year?'' word from ''calendars'' vocabulary. For example:
<
2011 leap-year? .</
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.</
=={{header|Forth}}==
<
dup 400 mod 0= if drop true exit then
dup 100 mod 0= if drop false exit then
4 mod 0= ;</
Or more simply (but always computing three "mod"):
<
=={{header|Fortran}}==
<
implicit none
Line 1,486:
end function leap_year
end program leap</
{{out}}
<pre> F T F T </pre>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,532:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> 1800 is not a leap year
Line 1,554:
=={{header|FutureBasic}}==
<
// In-line C function to generate random number in range
Line 1,605:
next
HandleEvents</
Output (results will vary for random years):
Line 1,654:
=={{header|Gambas}}==
<
Dim dDate As Date
Dim siYear As Short = InputBox("Enter a year", "Leap year test")
Line 1,664:
Message(siYear & sMessage)
End</
Output:
Line 1,672:
=={{header|GAP}}==
<
return (n mod 4 = 0) and ((n mod 100 <> 0) or (n mod 400 = 0));
end;
Line 1,679:
IsLeapYear := function(n)
return DaysInYear(n) = 366;
end;</
=={{header|Genie}}==
Dialect conversion from Vala entry.
<
/*
Leap year, in Genie
Line 1,696:
for year in years
status:string = year.is_leap_year() ? "" : "not "
stdout.printf("%d is %sa leap year.\n", year, status)</
{{out}}
Line 1,709:
=={{header|Go}}==
<
return year%400 == 0 || year%4 == 0 && year%100 != 0
}</
=={{header|Groovy}}==
Solution:
<
{{out}}
<pre style="height:30ex;overflow:scroll;">1904
Line 1,748:
=={{header|GW-BASIC}}==
{{works with|PC-BASIC|any}}
<
10 ' Leap year
20 DEF FN ISLEAPYEAR(Y%) = ((Y% MOD 4 = 0) AND (Y% MOD 100 <> 0)) OR (Y% MOD 400 = 0)
Line 1,760:
160 END
200 DATA 1900, 1994, 1996, 1997, 2000
</syntaxhighlight>
{{out}}
<pre>
Line 1,771:
=={{header|Harbour}}==
<
RETURN iif( nYear % 100 == 0, nYear % 400 == 0, nYear % 4 == 0 )</
=={{header|Haskell}}==
'''Simple version'''
<
import Control.Monad
import Control.Arrow
Line 1,784:
isleapsf j | 0==j`mod`100 = 0 == j`mod`400
| otherwise = 0 == j`mod`4</
'''Algorithmic'''
<
Example using isleap
<
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</
'''TDD version'''
<
isLeapYear::Int->Bool
Line 1,809:
,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]</
=={{header|Hy}}==
<
(and
(= (% y 4) 0)
(or
(!= (% y 100) 0)
(= (% y 400) 0))))</
=={{header|Icon}} and {{header|Unicon}}==
Gives leap year status for 2000,1900,2012 and any arguments you give
<
every y := !([2000,1900,2012]|||arglist) do
write("The year ",y," is ", leapyear(y) | "not ","a leap year.")
Line 1,828:
procedure leapyear(year) #: determine if year is leap
if (numeric(year) % 4 = 0 & year % 100 ~= 0) | (numeric(year) % 400 = 0) then return
end</
=={{header|J}}==
<
Example use:
<
0 1 0 1</
=={{header|Java}}==
Line 1,842:
Both values are printed in the output.
<
import java.text.MessageFormat;
Line 1,860:
}
</syntaxhighlight>
{{out}}
<pre>The year 1800 is leaper: false / false.
Line 1,874:
{{works with|Java|8}}
<
public class IsLeap {
Line 1,882:
}
}
</syntaxhighlight>
=={{header|JavaScript}}==
<
Or, by setting the day to the 29th and checking if the day remains
<
var isLeapYear = function (year) { return new Date(year, 1, 29).getDate() === 29; };</
=={{header|jq}}==
{{trans|Julia}}
<
. as $y | ($y%4) == 0 and ($y < 1582 or ($y%400) == 0 or ($y%100) != 0);</
'''Examples''':
<
value as $value
| ($value|f) | if . then empty else error("assertion violation: \($value) => \(.)") end;
Line 1,902:
((2100, 2014, 1900, 1800, 1700, 1499) | assert(.; leap|not))
</syntaxhighlight>
{{out}}
$ jq -n -f Leap_year.jq
Line 1,909:
{{works with|Julia|0.6}}
<
@assert all(isleap, [2400, 2012, 2000, 1600, 1500, 1400])
@assert !any(isleap, [2100, 2014, 1900, 1800, 1700, 1499])</
=={{header|K}}==
<
a@&leapyear'a:1900,1994,1996,1997,2000
1996 2000</
=={{header|Kotlin}}==
<
=={{header|Lasso}}==
<
#y % 400 == 0 ? return true
#y % 100 == 0 ? return false
Line 1,934:
isLeapYear(#test)
'\r'
^}</
{{out}}
Line 1,946:
=={{header|Liberty BASIC}}==
=== Simple method ===
<
print "leap"
else
Line 1,955:
function leap(n)
leap=date$("2/29/";n)
end function</
=== Calculated method ===
<
select case
case year mod 400 = 0
Line 1,971:
else
print year;" is not a leap year."
end if</
=={{header|Lingo}}==
<
return date(year, 2, 29).month=2
end</
=={{header|LiveCode}}==
<
return (year MOD 4 is 0) AND ((year MOD 400 is 0) OR (year MOD 100 is not 0))
end isLeapYear
Line 1,996:
1996 is true
1997 is false
2000 is true </
=={{header|LLVM}}==
<
; 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 2,092:
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 }</
{{out}}
<pre>1900 is not a leap year.
Line 2,101:
=={{header|Logo}}==
<
output equal? 0 modulo :n :d
end
to leapyear? :y
output ifelse multiple? :y 100 [multiple? :y 400] [multiple? :y 4]
end</
=={{header|Logtalk}}==
<
( mod(Year, 4) =:= 0, mod(Year, 100) =\= 0 ->
true
; mod(Year, 400) =:= 0
).</
=={{header|LOLCODE}}==
<
HAI 1.3
HOW IZ I Leap YR Year
Line 2,162:
KTHXBYE
</syntaxhighlight>
{{Out}}
<pre>1900 is NOT a leap year
Line 2,173:
=={{header|Lua}}==
<
return year%4==0 and (year%100~=0 or year%400==0)
end</
=={{header|Maple}}==
<
if not year mod 4 = 0 or (year mod 100 = 0 and not year mod 400 = 0) then
return false;
Line 2,184:
return true;
end if;
end proc:</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Dates are handled by built-in functions in the Wolfram Language
<syntaxhighlight lang
=={{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.
<
TrueFalse = (eomday(year,2) == 29);
end</
===Using Logical and modular functions===
<
=={{header|Maxima}}==
<
(mod(year, 100) # 0 or mod(year, 400) = 0))$</
=={{header|Mercury}}==
<
is_leap_year(Year) :-
( if Year mod 100 = 0 then Year mod 400 = 0 else Year mod 4 = 0 ).</
Usage:
<
:- interface.
Line 2,228:
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).</
=={{header|min}}==
{{works with|min|0.19.6}}
<
(((400 divisor?) (4 divisor?) (100 divisor? not)) cleave and or) :leap-year?</
=={{header|MiniScript}}==
<
return year%4==0 and (year % 100 or not year % 400)
end function</
=={{header|MIPS Assembly}}==
Pass year in a0, returns boolean in v0.
<
IsLeap: andi $a1, $a0, 3 #a0 is year to test
bnez $a1 NotLeap
Line 2,256:
NotLeap:li $v0, 0
jr $ra
</syntaxhighlight>
=={{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 С/П</
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 2,293:
Print(2000);
ReadChar
END LeapYear.</
=={{header|MUMPS}}==
<
QUIT ((X#4=0)&(X#100'=0))!((X#100=0)&(X#400=0))</
Usage: <pre>USER>W $SELECT($$ILY^ROSETTA(1900):"Yes",1:"No")
No
Line 2,307:
=={{header|Nanoquery}}==
{{trans|Python}}
<
if (year % 100 = 0)
return (year % 400 = 0)
Line 2,313:
return (year % 4 = 0)
end
end</
=={{header|Neko}}==
Translating from C
<syntaxhighlight lang="actionscript">/**
<doc><h2>Leap year, in Neko</h2></doc>
**/
Line 2,326:
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")</
{{out}}
Line 2,341:
=={{header|Nemerle}}==
Demonstrating implementation as well as use of standard library function.
<
using System.Console;
using Nemerle.Assertions;
Line 2,377:
DateTime.IsLeapYear(DateTime.Now.Year));
}
}</
{{out}}
<pre>2000 is a leap year: True
Line 2,394:
prior to the Gregorian cut-over and leap-year rules in the Julian calendar
are different to those for the Gregorian calendar.
<
options replace format comments java crossref savelog symbols nobinary
Line 2,438:
method isFalse public constant binary returns boolean
return \isTrue</
{{out}}
<pre>
Line 2,449:
=={{header|Nim}}==
<
let year = 1980
echo isLeapYear(year)
Line 2,460:
else: year mod 4 == 0
echo isLeapYear2(year)</
{{out}}
<pre>true
Line 2,466:
=={{header|NS-HUBASIC}}==
<
20 IF A-(A/100)*100=0 AND A-(A/400)*400<>0 THEN RESULT$="NOT "
30 PRINT "THAT'S "RESULT$"A LEAP YEAR."</
=={{header|Oberon-2}}==
<
PROCEDURE IsLeapYear(year: INTEGER): BOOLEAN;
BEGIN
Line 2,488:
END
END IsLeapYear;
</syntaxhighlight>
=={{header|Objeck}}==
<
class LeapYear {
function : Main(args : String[]) ~ Nil {
Line 2,517:
}
}
}</
=={{header|OCaml}}==
<
if (year mod 100) = 0
then (year mod 400) = 0
else (year mod 4) = 0</
Using Unix Time functions:
<
let tm =
Unix.mktime {
Line 2,534:
}
in
(tm.Unix.tm_mday = 29)</
=={{header|Oforth}}==
<syntaxhighlight lang
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
::routine isLeapYear
use arg year
d = .datetime~new(year, 1, 1)
return d~isLeapYear
</syntaxhighlight>
=={{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.
<
i_iyear AS INTEGER
):
Line 2,565:
1997 isLeapYear( 1997 ) SKIP
2000 isLeapYear( 2000 )
VIEW-AS ALERT-BOX.</
=={{header|Oz}}==
<
fun {IsLeapYear Year}
case Year mod 100 of 0 then
Line 2,583:
{System.showInfo Y#" is NOT a leap year."}
end
end</
{{out}}
<pre>1900 is NOT a leap year.
Line 2,591:
=={{header|PARI/GP}}==
<
if(n%400==0, return(1));
if(n%100==0, return(0));
n%4==0
};</
Alternate version:
<
{{works with|PARI/GP|2.6.0 and above}}
<
if(n%4,0,
n%100,1,
n%400,0,1
)
};</
=={{header|Pascal}}==
{{works with|Free Pascal}}
<
uses
sysutils;//includes isLeapYear
Line 2,626:
TestYear(2100);
TestYear(1904);
end.</
Output:
<pre>1900 is NO leap year
Line 2,634:
=={{header|Perl}}==
<
my $year = shift;
if ($year % 100 == 0) {
Line 2,640:
}
return ($year % 4 == 0);
}</
Or more concisely:
<
Alternatively, using functions/methods from CPAN modules:
<
print Date_LeapYear(2000);
Line 2,657:
use DateTime;
my $date = DateTime->new(year => 2000);
print $date->is_leap_year();</
=={{header|Phix}}==
Available as an auto-include, implemented as:
<!--<
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</
=={{header|PHP}}==
<
function isLeapYear($year) {
if ($year % 100 == 0) {
Line 2,674:
}
return ($year % 4 == 0);
}</
With <code>date('L')</code>:
<
function isLeapYear($year) {
return (date('L', mktime(0, 0, 0, 2, 1, $year)) === '1')
}</
=={{header|Picat}}==
<
foreach(Y in [1600,1700,1899,1900,2000,2006,2012])
println(Y=cond(leap_year(Y),leap_year,not_leap_year))
Line 2,691:
(Year mod 4 == 0, Year mod 100 != 0)
;
Year mod 400 == 0. </
{{out}}
Line 2,703:
=={{header|PicoLisp}}==
<
(bool (date Y 2 29)) )</
{{out}}
<pre>: (isLeapYear 2010)
Line 2,719:
=={{header|PL/I}}==
<
dcl year fixed bin (31);
Line 2,729:
else
put skip edit(year, 'is not a leap year') (p'9999b', a);
end;</
{{out}}
Line 2,743:
=={{header|PL/M}}==
<
/* CP/M BDOS SYSTEM CALL */
Line 2,786:
END;
EOF</
{{out}}
<pre>
Line 2,807:
=={{header|PostScript}}==
<
dup dup
4 mod 0 eq % needs to be divisible by 4
Line 2,816:
400 mod 0 eq % or by 400
or
} def</
=={{header|PowerShell}}==
<
[System.DateTime]::IsLeapYear( $Year )</
=={{header|Prolog}}==
{{Works with|SWI-Prolog}}
<
partition(is_leap_year, L, LIn, LOut),
format('leap years : ~w~n', [LIn]),
Line 2,833:
R100 is Year mod 100,
R400 is Year mod 400,
( (R4 = 0, R100 \= 0); R400 = 0).</
{{out}}
<
leap years : [1996,2000]
not leap years : [1900,1994,1997]
L = [1900,1994,1996,1997,2000].</
There is an handy builtin that simplifies a lot, ending up in a simple query:
<syntaxhighlight 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>
=={{header|PureBasic}}==
<
If (Year%4=0 And Year%100) Or Year%400=0
ProcedureReturn #True
Line 2,854:
ProcedureReturn #False
EndIf
EndProcedure</
=={{header|Python}}==
<
calendar.isleap(year)</
or
<
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0</
Asking for forgiveness instead of permission:
<
def is_leap_year(year):
Line 2,872:
except ValueError:
return False
return True</
=={{header|Q}}==
<
=={{header|Quackery}}==
{{trans|Forth}}
<
dup 100 mod 0 = iff [ drop false ] done
4 mod 0 = ] is leap? ( n --> b )</
=={{header|R}}==
<
ifelse(year%%100==0, year%%400==0, year%%4==0)
}
Line 2,890:
for (y in c(1900, 1994, 1996, 1997, 2000)) {
cat(y, ifelse(isLeapYear(y), "is", "isn't"), "a leap year.\n")
}</
{{out}}
<pre>
Line 2,901:
=={{header|Racket}}==
<
(and (zero? (modulo y 4)) (or (positive? (modulo y 100)) (zero? (modulo y 400)))))</
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2010.07}}
<syntaxhighlight lang="raku"
In Rakudo 2010.07, <code>Date.is-leap-year</code> is implemented as
<syntaxhighlight lang="raku"
$y %% 4 and not $y %% 100 or $y %% 400
}</
=={{header|Rapira}}==
<
if (year /% 100) = 0 then
return (year /% 400) = 0
fi
return (year /% 4) = 0
end</
=={{header|Raven}}==
<
$year 100 % 0 = if
$year 400 % 0 =
$year 4 % 0 =</
=={{header|REBOL}}==
<
{Returns true if the specified year is a leap year; false otherwise.}
year [date! integer!]
Line 2,942:
div?: func [n] [zero? year // n]
to logic! any [all [div? 4 not div? 100] div? 400]
]</
=={{header|Retro}}==
<
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? ;</
=={{header|REXX}}==
===local variables===
<
return yr//400==0 | (yr//100\==0 & yr//4==0)</
===with short-circuit===
The REXX language doesn't support short-circuits, so here is a version that does a short-circuit.
<
if yr//4\==0 then return 0 /*Not ÷ by 4? Not a leap year.*/
return yr//400==0 | yr//100\==0</
===no local variables===
This version doesn't need a PROCEDURE to hide local variable(s) [because there aren't any local variables],
<br>but it does invoke the '''ARG''' BIF multiple times.
<
return arg(1)//400==0 | arg(1)//100\==0</
===handles 2 digit year===
Line 2,971:
<br>the current century is assumed (i.e., 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).
<
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. */</
=={{header|Ring}}==
<
give year
leap = isLeapYear(year)
Line 2,988:
but (year % 4) = 0 return true
else return false ok
</syntaxhighlight>
=={{header|RPG}}==
Line 2,994:
{{works with|RPGIII|}}
<
C *ENTRY PLIST
C PARM YEAR 40 input (year)
Line 3,020:
C*
C DONE TAG
C SETON LR</
=={{header|Ruby}}==
<
Date.leap?(year)</
The leap? method is aliased as gregorian_leap? And yes, there is a julian_leap? method.
=={{header|Run BASIC}}==
<
=={{header|Rust}}==
<
let factor = |x| year % x == 0;
factor(4) && (!factor(100) || factor(400))
}</
=={{header|S-BASIC}}==
Since S-BASIC has no MOD operator or function, we have to supply one.
<
rem - compute p mod q
function mod(p, q = integer) = integer
Line 3,069:
end
</syntaxhighlight>
{{out}}
<pre>
Line 3,091:
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.
<
new java.util.GregorianCalendar().isLeapYear(year)</
===JDK 8===
Using JSR-310 java.time.
<
===Implementation===
Line 3,102:
For proleptic Gregorian calendar:
<
//or use Java's calendar class
Line 3,109:
c.setGregorianChange(new java.util.Date(Long.MinValue))
c.isLeapYear(year)
}</
=={{header|Scheme}}==
<
(apply (lambda (a b c) (or a (and (not b) c)))
(map (lambda (m) (zero? (remainder n m)))
'(400 100 4))))</
=={{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.
<
return (year rem 4 = 0 and year rem 100 <> 0) or year rem 400 = 0;</
Original source: [http://seed7.sourceforge.net/algorith/date.htm#isLeapYear]
=={{header|Sidef}}==
<
if (year %% 100) {
return (year %% 400);
}
return (year %% 4);
}</
or a little bit simpler:
<
=={{header|Smalltalk}}==
Smalltalk has a built-in method named <tt>isLeapYear</tt>:
<
Date today isLeapYear.
</syntaxhighlight>
=={{header|SNOBOL4}}==
Predicate leap( ) succeeds/fails, returns nil.
<
leap eq(remdr(yr,400),0) :s(return)
eq(remdr(yr,100),0) :s(freturn)
Line 3,154:
yr = '1900'; eval(test)
yr = '2000'; eval(test)
end</
{{out}}
<pre>0: 1066
Line 3,162:
=={{header|Standard ML}}==
<
y mod (if y mod 100 = 0 then 400 else 4) = 0</
=={{header|Stata}}==
Line 3,169:
Given a dataset with a "year" variable, generate a variable "leap" which is 1 for a leap year, 0 otherwise.
<
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}}==
<
return year.isMultiple(of: 100) ? year.isMultiple(of: 400) : year.isMultiple(of: 4)
}
Line 3,180:
[1900, 1994, 1996, 1997, 2000].forEach { year in
print("\(year): \(isLeapYear(year: year) ? "YES" : "NO")")
} </
{{out}}
<pre>1900: NO
Line 3,191:
=={{header|Tcl}}==
The "classic" modulo comparison:
<
return [expr {($year % 4 == 0) && (($year % 100 != 0) || ($year % 400 == 0))}]
}
Line 3,197:
isleap1 1989 ;# => 0
isleap1 1900 ;# => 0
isleap1 2000 ;# => 1</
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.)
<
return [expr {[clock format [clock scan "$year-02-29" -format "%Y-%m-%d"] -format "%m-%d"] eq "02-29"}]
}
Line 3,205:
isleap2 1989 ;# => 0
isleap2 1900 ;# => 0
isleap2 2000 ;# => 1</
=={{header|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</
{{out}}
<pre>
Line 3,225:
=={{header|uBasic/4tH}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="text">DO
INPUT "Enter a year: "; y
IF FUNC(_FNleap(y)) THEN
Line 3,236:
_FNleap Param (1)
RETURN ((a@ % 4 = 0) * ((a@ % 400 = 0) + (a@ % 100 # 0)))</
=={{header|UNIX Shell}}==
Original Bourne:
<
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;
}</
Using GNU date(1):
<
date -d "$1-02-29" >/dev/null 2>&1;
}</
Defining a bash function <tt>is_leap</tt> which accepts a YEAR argument, and uses no IO redirection, nor any extra processes.
<
local year=$(( 10#${1:?'Missing year'} ))
(( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) )) && return 0
return 1
}</
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,265:
<!-- 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 -->
<
cal 02 $1 | grep -q 29
}
</syntaxhighlight>
=={{header|Ursa}}==
This program takes a year as a command line argument.
<
set year (int args<1>)
if (= (mod year 4) 0)
Line 3,282:
else
out year " is not a leap year" endl console
end if</
Output in Bash:
<pre>$ ursa leapyear.u 1900
Line 3,290:
=={{header|Vala}}==
<
DateYear[] years = { 1900, 1994, 1996, 1997, 2000 };
foreach ( DateYear year in years ) {
Line 3,296:
print (@"$year is $(status)a leap year.\n");
}
}</
{{out}}
<pre>
Line 3,307:
=={{header|VBA}}==
<
Leap_year = (Month(DateSerial(year, 2, 29)) = 2)
End Function</
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function IsLeapYear(yr)
IsLeapYear = False
Line 3,330:
End If
Next
</syntaxhighlight>
{{Out}}
Line 3,343:
=={{header|Vedit macro language}}==
<
#2 = (#1 % 4 == 0) && ((#1 % 100 != 0) || (#1 % 400 == 0))
if (#2) {
Line 3,350:
Message(" is not leap year\n")
}
}</
The following version requires Vedit 6.10 or later:
<
if (Is_Leap_Year(#1)) {
Message(" is leap year\n")
Line 3,359:
Message(" is not leap year\n")
}
}</
=={{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
Line 3,373:
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
Line 3,383:
Next i
End Sub
</syntaxhighlight>
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Sub Main()
Line 3,395:
End Sub
End Module</
{{out}}
<pre>1900 is not a leap year.
Line 3,403:
=={{header|Vlang}}==
<
return year %400 ==0 || (year%4 ==0 && year%100!=0)
}
Line 3,414:
}
}
</syntaxhighlight>
Returns:
<pre>1952
Line 3,434:
=={{header|WDTE}}==
<
let multiple of n => == (% n of) 0;
Line 3,443:
multiple 4 => '';
default => ' not';
}) -- io.writeln io.stdout;</
=={{header|WebAssembly}}==
First, with syntactic sugar that allows us to put opcode arguments after the opcode itself:
<
;; 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)
Line 3,460:
)
(export "isLeapYear" (func $isLeapYear))
)</
And then the same code, without the syntactic sugar:
<
;; 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)
Line 3,484:
)
(export "isLeapYear" (func $isLeapYear))
)</
=={{header|Wortel}}==
<
isLeapYear !?{\~%%1H \~%%4H \~%%4}
!-isLeapYear @range[1900 2000]
}</
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}}==
<
return ((y % 4 == 0) && (y % 100!= 0)) || (y % 400 == 0)
}
Line 3,507:
if (c % 15 == 0) System.print()
}
}</
{{out}}
Line 3,518:
=={{header|X86 Assembly}}==
Using FASM syntax. Leaf function fits nicely into your program.
<
; Input year as signed dword in EAX
IsLeapYear:
Line 3,534:
.100:
test eax,11b
retn ; 1% : ZF=?, leap year if EAX%400=0</
=={{header|XLISP}}==
<
(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)))</
{{out}}
<pre>(#T #T () #T () () #T #T () #T)</pre>
=={{header|XPL0}}==
<
int Y;
[if rem(Y/100)=0 then return rem(Y/400)=0;
return rem(Y/4)=0;
];</
=={{header|Yabasic}}==
<
if mod(year, 4) <> 0 then return false : fi
if mod(year, 100) = 0 and mod(year, 400) <> 0 then return false : fi
Line 3,572:
if mod(year, 4) = 3 then print : fi
next year
end</
=={{header|Yorick}}==
This solution is vectorized and can be applied to scalar or array input.
<
return ((y % 4 == 0) & (y % 100 != 0)) | (y % 400 == 0);
}</
Interactive example usage:
<pre>> is_leap(1988)
Line 3,587:
=={{header|zkl}}==
<
T(1988,1989,1900,2000).apply(Time.Date.isLeapYear)
//-->L(True,False,False,True)</
{{omit from|GUISS|Only the operator can read the calendar}}
|