Question:
Java: Leap Year list?
CM.
2009-09-29 19:59:43 UTC
So I am having some trouble with this problem that involves calculating all the leap years between two given years. I DON'T need to input the years; just hard code them into the program.

The starting year is 1800, and the ending year is 2100.

The output needs to look like this:
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 2024
2028 2032 2036 2040 2044
2048 2052 2056 2060 2064
2068 2072 2076 2080 2084
2088 2092 2096

I've never really programmed anything, so don't make any assumptions. Thanks.
Four answers:
Andrew
2009-09-29 20:43:02 UTC
ArrayList aList = new ArrayList();

for (int i=1800;i<2100;i+=4)

{

if (i%4==0)

if(i%100!=0)

aList.add(i);

}



/*this instantiates an Integer ArrayList and adds all the leap years to the data structure. Good Luck! :)

then you can output it anyway you want to

*/
Ratchetr
2009-09-29 20:10:16 UTC
Lots of programmers have gotten this one wrong.



Hard coded tables aren't really the best way to do it. What happens in 2100? You don't care, right? Nobody will be running this code then. Probably not....but remember the Y2K issues? Same thinking...



The algorithm for leap year isn't all that hard. See link.



ETA: Thank you Andrew for proving my point: Lots of programmers get this wrong. Andrew's code will leave 2000 out of the list. But Y2K WAS a leap year. Who would have thought that people would still be writing Y2K bugs in 2009?
Mark aka jack573
2009-10-03 14:25:19 UTC
You may be learning (and obviously are), that's why you are hard coding the years in. Don't worry about the other answerers saying it is wrong.



Ok, here is the code. It is very simple. Since you know every leap year is 4 years aparrt, but some years that are 4 apart may not be leap years, you can increment the year by 4.



public class LeapYears

{

public static void main(String[] args)

{

int count = 1;

for (int year = 1800; year <= 2100; year = year + 4)

{

if (year % 400 == 0)

{

System.out.print(year + " ");

count++;

}

else if (year % 100 != 0)

{

System.out.println(year + " ");

count++;

}

if (count % 5 == 0)

{

System.out.println(); // Make a new line after every 5 years printed.

}

}

}



Good luck with it.
casida
2016-11-04 16:44:38 UTC
There are 2 procedures. at the start you will use the bounce year rule "each 4 years, as properly each hundred years, as properly each 4 hundred". int isLeapYear = ((year % 4 == 0) && (year % one hundred != 0)) || (year % 4 hundred == 0); think ofyou've have been given to need that rule is appropriate and covers sufficient time. Secondly you would be counted on the built in approach purposes. build a struct tm and assign it midday on 28 Februrary of the year in question and use mktime to get a corrsponding time_t, then do the equivalent for a million March of the year. Subtract the two time_t values and be conscious in the event that they symbolize greater desirable like 40 8 hours and 24 hours. the 2nd approach is somewhat greater fiddly to put in writing, inspite of the shown fact that a procedures much less confusing to verify and finally end up appropriate. Plus it does now not be counted on you making use of the western calendar, it particularly is made to artwork for various calendars as stable.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...