Determining Leap Years
Determining whether a year is a leap year is a common programming problem. By definition, a leap year is divisible by four, but not by one hundred, unless it is divisible by four hundred.
One approach to solving this problem is to use the modulo operator. For example, the following code checks whether a year is divisible by four and not by one hundred:
However, this code does not account for the special case where a year is divisible by four hundred. To handle this, you can add an additional condition:
While this code is more accurate, it can be simplified using the calendar.isleap function, which returns True if the year is a leap year and False otherwise:
This last solution is the most concise and readable, and it provides the correct result.
The above is the detailed content of How to Determine the Leap Year Status of a Given Year?. For more information, please follow other related articles on the PHP Chinese website!