Drill

ProblemsC# › dates

Is it a leap year

easydatesMathC#

A billing schedule needs to know whether February has 29 days.

IsLeapYear(year: int) → bool

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public bool IsLeapYear(int year) {
    
}

Worked examples

CallResult
IsLeapYear(2024)true
IsLeapYear(2023)false
IsLeapYear(1900)false
IsLeapYear(2000)true

Hint

The 400 rule wins over the 100 rule, which wins over the 4 rule. Order the checks accordingly.

Reference solution in C#
public bool IsLeapYear(int year) {
    if (year < 1) return false;
    if (year % 400 == 0) return true;
    if (year % 100 == 0) return false;
    return year % 4 == 0;
}

The same problem in another language

More dates problems in C#