programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Different way of thinking about Leap

John Peters
Greenhorn
Posts: 18
For some reason I couldn't wrap my mind around how the leap years should be tested. After using three sheets of paper to write out how the logic would make sense, and using Wikipedia, I think I've come up with something that makes more sense in my mind. I hope it helps someone else:

In simple terms:

Where the program should terminate kept hanging me up.

Good help from Wikipedia:

...most years whose division by 4 equals an integer are leap years.
Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.

Marilyn de Queiroz
Sheriff
Posts: 9080
12
Originally posted by John Peters:
If the year is divisible by 400, than it's true and program should exit.
If the year is not divisible by 100 but IS (and) divisible by 4, than it's true and should exit.
Otherwise it's false and the program should exit.

I think I would change it just a little bit ...
If the year is divisible by 400, than it's true -- final answer
If the year is divisible by 4 but (and) is not divisible by 100, than it's true -- final answer
Otherwise it's false -- final answer