Leap year: Difference between revisions
(→{{header|J}}: simplify) |
|||
Line 11: | Line 11: | ||
return (year % 4 == 0); |
return (year % 4 == 0); |
||
}</lang> |
}</lang> |
||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68|Revision 1 - no extensions to language used}} |
|||
{{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 FORMATted transput}} |
|||
<lang algol68>MODE YEAR = INT, MONTH = INT, DAY = INT; |
|||
PROC year days = (YEAR year)DAY: # Ignore 1752 CE for the moment # |
|||
( month days(year, 2) = 28 | 365 | 366 ); |
|||
PROC month days = (YEAR year, MONTH month) DAY: |
|||
( month | 31, |
|||
28 + ABS(year MOD 4 NE 0 OR year MOD 400 EQ 0), |
|||
31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
|||
PROC is leap year = (YEAR year)BOOL: year days(year)=366; |
|||
test:( |
|||
[]INT test cases = (1900, 1996, 1997, 2000); |
|||
FOR i TO UPB test cases DO |
|||
YEAR year = test cases[i]; |
|||
printf(($g(0)" is "b("","not ")"a leap year."l$, year, is leap year(year))) |
|||
OD |
|||
)</lang> |
|||
Output: |
|||
<pre> |
|||
1900 is not a leap year. |
|||
1996 is not a leap year. |
|||
1997 is a leap year. |
|||
2000 is a leap year. |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |
Revision as of 21:43, 20 July 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Determine whether a given year is a leap year.
See Also
ActionScript
<lang actionscript>public function isLeapYear(year:int):Boolean {
if (year % 100 == 0) { return (year % 400 == 0); } return (year % 4 == 0);
}</lang>
ALGOL 68
<lang algol68>MODE YEAR = INT, MONTH = INT, DAY = INT;
PROC year days = (YEAR year)DAY: # Ignore 1752 CE for the moment #
( month days(year, 2) = 28 | 365 | 366 );
PROC month days = (YEAR year, MONTH month) DAY:
( month | 31, 28 + ABS(year MOD 4 NE 0 OR year MOD 400 EQ 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
PROC is leap year = (YEAR year)BOOL: year days(year)=366;
test:(
[]INT test cases = (1900, 1996, 1997, 2000); FOR i TO UPB test cases DO YEAR year = test cases[i]; printf(($g(0)" is "b("","not ")"a leap year."l$, year, is leap year(year))) OD
)</lang> Output:
1900 is not a leap year. 1996 is not a leap year. 1997 is a leap year. 2000 is a leap year.
J
<lang j>isLeap=: 0 -/@:= 4 100 400 |/ ]</lang>
Example use:
<lang> isLeap 1900 1996 1997 2000 0 1 0 1</lang>
Java
<lang java>public static boolean isLeapYear(int year){
if(year % 100 == 0) return year % 400 == 0; return year % 4 == 0;
}</lang>
JavaScript
<lang javascript>function isLeapYear(year) {
if (year % 100 == 0) { return (year % 400 == 0); } return (year % 4 == 0);
}</lang>
PHP
<lang php><?php function isLeapYear(year) {
if (year % 100 == 0) { return (year % 400 == 0); } return (year % 4 == 0);
}</lang>
With date('L')
:
<lang php><?php function isLeapYear(year) {
return (date('L', mktime(0, 0, 0, 2, 1, year)) === '1')
}</lang>
Python
<lang python>def is_leap_year(year):
if year % 100 == 0: return year % 400 == 0 return year % 4 == 0
</lang>
Asking for forgiveness instead of permission:
<lang python>import datetime
def is_leap_year(year):
try: datetime.date(year, 2, 29) except ValueError: return False return True
</lang>