Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
1.1.
Sample Examples
2.
Solution Approach
2.1.
Implementation in C++
2.2.
Complexity Analysis
3.
FAQs
4.
Key takeaways
Last Updated: Mar 27, 2024
Easy

Check Leap Year

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction 

This blog will discuss the approach to check whether the given number is a leap year or not. 

Before understanding the approach and code, let's first discuss leap year.

All years which are perfectly divisible by 4 are leap years except for century years (years ending with 00), which are leap years only if they are perfectly divisible by 400. 

Sample Examples

Example 1:

Input: 2006
Output: 2006 is not a leap number. 
Explanation: 2006 is not divisible by 4, hence it is not a leap year. 

 

Example 2:

Input: 2000
Output: 2000 is a leap year
Explanation: 2000 is divisible by 4 as well as 400, hence it is a leap year.  

Solution Approach

The solution to this problem is very trivial, we will check if the number is divisible by 4, and not 100, or if the number is divisible by 400, then it is a leap year. 

Implementation in C++

// c++ program to check whether the given number is leap year or not
#include<bits/stdc++.h>
using namespace std;
bool isLeapYear(int n){
    // if the condition for leap year is satisfied, return true
    if((n%4==0) && (n%100!=0 || n%400==0))
        return true;
    // return false otherwise
    return false;
}
int main(){
    int n = 1604;
    if(isLeapYear(n))
        cout << n << " is a leap year" << endl;
    else
        cout << n << " is not a leap year" << endl;
}

 

Output:

1604 is a leap year

 

Try and compile with online c++ compiler.

Complexity Analysis

Time Complexity: O(1)

Explanation: only constant time operations are performed. 

Space ComplexityO(1)

Explanation: We are not using any explicit space, so O(1) time complexity. 

Also readDecimal to Binary c++

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. How many days are there in a leap year?  
    A leap year has 366 days (the extra day is the 29th of February), and it comes after every four years.  
     
  2. What are prime numbers? 
    Prime numbers are whole numbers bigger than 1 with just two factors: 1 and the number itself, according to mathematics. Only the number 1 or the number itself divides prime numbers. 
     
  3. Is 1604 a leap year?
    Yes 1604 is divisible by 4, hence it is a leap year. 

Key takeaways

In this article, we discussed the problem in which we are given a number n, and we need to check whether the given number is a leap year or not. We hope you understand the problem and solution properly. Now you can do more similar questions. 

If you are a beginner, interested in coding, and want to learn DSA, you can look for our guided path for DSA, which is free! 

Check out this problem - Check If A String Is Palindrome

Thank you for reading. 

Until then, Keep Learning and Keep Coding.

Previous article
Fibonacci series
Next article
Program to check whether a number is a Palindrome or not
Live masterclass