Check if given year is a leap year or not

In this post we will solve the problem “Check if given year is a leap year or not”.

Problem Statement: Check if the given year is a leap year or not.

Examples:

Example 1:
Input: 1996
Output: Yes
Explanation: Since 1996 is a leap year answer is “Yes”.

Example 2:
Input: 2000
Output: Yes

Explanation: Since 2000 is a leap year answer is “Yes”.

Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Intuition: A year is a leap year only if it satisfies the following condition.

  • The year is divisible by 400
  • The year is divisible by 4 but not by 100

Approach: Check if the year is divisible by 4 or 400 but not by 100 then it is a leap year.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
bool yyear(int year)
{
    if(year % 400 == 0)
    return true;
    if(year % 100 == 0)
    return false;
    if(year % 4 == 0)
    return true;
    return false;
}
int main()
{
    int year=1996;
    if(yyear(year))
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
}

Output:

Yes

Time Complexity: O(1).
Space Complexity: O(1).

Java Code

import java.util.*;
class TUF{
static boolean yyear(int year)
{
    if(year % 400 == 0)
    return true;
    if(year % 100 == 0)
    return false;
    if(year % 4 == 0)
    return true;
    return false;
}
public static void main(String args[])
{
    int year=1996;
    if(yyear(year)==true)
    System.out.println("Yes");
    else
    System.out.println("No");
}
}

Output:

Yes

Time Complexity: O(1).
Space Complexity: O(1).

Solution 2: Using only one if-else statement.

Approach: If a year is divisible by 4 or 400 but not by 100 then it is a leap year.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
int main() {
	int year = 2000;
	if (((year % 4 == 0) && (year % 100 != 0)) ||(year % 400 == 0)) {
		cout << "Yes" << "\n";
	}
	else {
		cout << "No" << "\n";
	}
}

Output:

Yes

Time Complexity: O(1).
Space Complexity: O(1).

Java Code

public class Main {
  public static void main(String args[]) {
    int year = 1992;
    if (((year % 4 == 0) && (year % 100 != 0)) ||(year % 400 == 0)) {
      System.out.println("Yes");
    } else {
      System.out.println("No");
    }
  }
}

Output:

Yes

Time Complexity: O(1).
Space Complexity: O(1).

Solution 3: Using macros

Macros: Macros are preprocessors. We write logic and name it using “#define”.Whenever this name is called in the code, the compiler performs the defined logic.

Approach:

  • Use ‘#define’ and give the name IsLeapYear.
  • Pass the parameter year.
  • Now check if the year is divisible by 4 or 400 but not by 100.
  • If it is true the macros will 1,else it will return 0.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
#define isLeapYear(y) ((y%400==0) ||  (y%4==0 && y%100!=0))
int main() {
	int year = 2024;
	if (isLeapYear(year)) {
		cout << "Yes";
	}
	else {
		cout << "No";
	}
}

Output:

Yes

Time Complexity: O(1).
Space Complexity: O(1).

Special thanks to Pranav Padawe for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article