Ad

Which years are leap years?
To be a leap year, the year number must be divisible by four – except for end-of-century years, which must be divisible by 400. This means that the year 2000 was a leap year, although 1900 was not. 2020, 2024 and 2028 are all leap years.

Code
Diff
  • using System;
    
    public class LeapYears
    {
      public static bool IsLeapYear(int year)
      {
        // End of a century must be divisible by 400
        if(year.ToString().EndsWith("00") && year % 400 == 0)
          return true;
        // Not the end of a century must be divisible by 4
        else if(!year.ToString().EndsWith("00") && year % 4 == 0)
          return true;
        else
          return false;
      }
    }
    • using System;
    • public class LeapYears
    • {
    • // Works but hurts
    • public static bool IsLeapYear(int y) => (y % 400 == 0) ? true : (y % 100 == 0 && y % 400 != 0) ? false : (y % 4 == 0 && y % 100 != 0) ? true : false;
    • public static bool IsLeapYear(int year)
    • {
    • // End of a century must be divisible by 400
    • if(year.ToString().EndsWith("00") && year % 400 == 0)
    • return true;
    • // Not the end of a century must be divisible by 4
    • else if(!year.ToString().EndsWith("00") && year % 4 == 0)
    • return true;
    • else
    • return false;
    • }
    • }