Check if a given Year is Leap Year (original) (raw)

Last Updated : 6 Jan, 2025

You are given an Integer **n. Return **true if It is a Leap Year otherwise return **false. A **leap year is a year that contains an additional day, February 29th, making it 366 days long instead of the usual 365 days. Leap years are necessary to keep our calendar in alignment with the Earth's revolutions around the Sun.

Note: A year is a leap year if "any one of " the following conditions are satisfied:

  1. The year is multiple of 400.
  2. The year is a multiple of 4 and not a multiple of 100.

**Example:

**Input: n = 4
**Output: true
**Explanation: 4 is not divisible by 100 and is divisible by 4 so its a leap year

**Input: n = 2021
**Output: false
**Explanation: 2021 is not divisible by 100 and is also not divisible by 4 so its not a leap year

Try It Yourselfredirect icon

Expected Approach - O(1) Time and O(1) Space

The basic idea is to check each number by dividing from 4, then again divide with 100, if we don't get 0 reminder then it is a Leap Year otherwise it is not.

C++ `

#include using namespace std;

bool checkYear(int n) {

// Check if n is divisible by 4 if (n % 4 == 0) {

  // If it's divisible by 100, it should also be 
  // divisible by 400 to be a leap year
  if (n % 100 == 0) {
     return n % 400 == 0;
  }
  return true;

} return false; }

int main() { int year = 2000; if (checkYear(year)) { cout << "true" << endl; } else { cout << "false" << endl; } return 0; }

C

#include <stdio.h>

int checkYear(int n) {

// Check if n is divisible by 4
if (n % 4 == 0) {
  
    // If it's divisible by 100, it should also be 
    // divisible by 400 to be a leap year
    if (n % 100 == 0) {
        return n % 400 == 0;
    }
    return 1;
}
return 0;

}

int main() { int year = 2000; if (checkYear(year)) { printf("true\n"); } else { printf("false\n"); } return 0; }

Java

class GfG { static boolean checkYear(int n) {

  // Check if n is divisible by 4
  if (n % 4 == 0) {
    
     // If it's divisible by 100, it should also be
     // divisible by 400 to be a leap year
     if (n % 100 == 0) {
        return n % 400 == 0;
     }
     return true;
  }
  return false;

}

public static void main(String[] args) { int year = 2000; if (checkYear(year)) { System.out.println("true"); } else { System.out.println("false"); } } }

Python

def checkYear(n):

# Check if n is divisible by 4
if n % 4 == 0:
  
    # If it's divisible by 100, it should also be 
    # divisible by 400 to be a leap year
    if n % 100 == 0:
        return n % 400 == 0
    return True
return False

if name == "main": year = 2000 if checkYear(year): print("true") else: print("false")

C#

using System;

class GfG { static bool CheckYear(int n) {

    // Check if n is divisible by 4
    if (n % 4 == 0) {
      
        // If it's divisible by 100, it should also be 
        // divisible by 400 to be a leap year
        if (n % 100 == 0) {
            return n % 400 == 0;
        }
        return true;
    }
    return false;
}

static void Main() {
    int year = 2000;
    if (CheckYear(year)) {
        Console.WriteLine("true");
    } else {
        Console.WriteLine("false");
    }
}

}

JavaScript

function checkYear(n) {

// Check if n is divisible by 4
if (n % 4 === 0) {

    // If it's divisible by 100, it should also be 
    // divisible by 400 to be a leap year
    if (n % 100 === 0) {
        return n % 400 === 0;
    }
    return true;
}
return false;

}

// Driver Code let year = 2000; if (checkYear(year)) { console.log("true"); } else { console.log("false"); }

`

**Time Complexity : O(1)
**Auxiliary Space: O(1)