Leap Year | Practice | GeeksforGeeks

For an input year N, find whether the year is a leap or not. 
 
Example 1:
Input:
N = 4
Output:
1
Explanation:
4 is not divisible by 100
and is divisible by 4 so its
a leap year
Example 2:
Input:
N = 2021
Output:
0
Explanation:
2021 is not divisible by 100
and is also not divisible by 4
so its not a leap year

Your Task:
You don't need to read input or print anything. Your task is to complete the function isLeap() which takes an integer N as input parameter and returns 1 is N is a leap year and 0 otherwise.
 
Expected Time Complexity: O(1)
Expected Space Complexity: O(1)

Constraints:
1<=N<=9999
+1
upadhyayshikhar063 months ago
int isLeap(int N)
{      return(((N % 4 == 0) &&(N % 100!= 0))||(N % 400 == 0));          }
0
badgujarsachin835 months ago
int isLeap(int N){
        //code here
        if(N>=400 && N%400==0){
            return 1;
        }
        if(N%4==0 && N%100!=0){
            return 1;
            
        }
        return 0;
    }
-1
xasha1227996 months ago
/*
1. Leap Year:
A year is a leap year if the following conditions are satisfied:
The year is multiple of 400.
The year is multiple of 4 and not multiple of 100.
*/
 
#include <bits/stdc++.h>
using namespace std;
 
// Function Declarations
 
// Driver Code
int main()
{
int N;
cin >> N;
isLeap(N);
 
return 0;
} // Main Ends
 
Function Definition
 
int isLeap(int N)
{
// Basically we, need to check for 3 conditions: Nested Loops is what is required to evaluate either a year is leap or not
 
if (N % 4 == 0)
{ // come here if year passed is divisible by 4
if (N % 100 == 0)
{ // come here if year passed is divisible by 100
if (N % 400 == 0)
{ // come here if year passed is divisible by 400
cout << "leap year";
}
else
{ // come here if year passed is divisible by 100 but not by 400
cout << "not leap year";
}
}
else
{ // come here if year passed is divisible by 4 and not divisible by 100
cout << "leap year";
}
}
else
{ // come here if year passed is not divisible by 4
cout << "not leap year";
}
}
 
OR
 
int isLeap(int N)
{
// 4 and 100 walli condition for normal years and 400 waali condition is for century year's
if ((N % 4 == 0 and N % 100 != 0) or (N % 400 == 0))
{
return 1;
}
else
{
return 0;
}
}
-3
Yogeshwari Harode10 months ago
Yogeshwari Harode
Leap Yearhttps://github.com/yogeshwa...
-2
Kiran Kumar Senapati11 months ago
Kiran Kumar Senapati
if((N%100!=0 and N%4==0)|| N%400==0)            return 1;        else            return 0;    }
-1
Debojyoti Sinha11 months ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
class Solution{    public:    int isLeap(int N)    {        if(N % 100 == 0)        {            return N % 400 == 0 ? 1 : 0;        }                return N % 4 == 0 ? 1 : 0;    }};
-1
Anshul Singh1 year ago
Anshul Singh
int isLeap(int N){        if((N%100!=0 && N%4==0)|| N%400==0){            return 1;        }        else{            return 0;        }    }
-1
Mysterious Anonimity1 year ago
Mysterious Anonimity
Shortest solution:-if((N%100!=0 && N%4==0) || N%400==0)            return 1;        else return 0;
-1
ariv1 year ago
ariv
In the above program, if the year is divisible by 4, then it is checked if it is divisible by 100. If it is divisible by 100, then it is checked if it is divisible by 400. It yes, then the year is a leap year. Otherwise, it is not.
0
Nikhil Singh
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.