Last Updated: 18 Nov, 2020

Leap Year

Easy
Asked in companies
SAP LabsCuemath

Problem statement

You are given a year in the form of an integer 'N', and your task is to check whether the given year is a leap year or not.

Note:

1) A year is said to be a leap year if it has 366 days. Normal years have 365 days.

2) The integer 'N' does not contain any leading zeros.
Input Format:
The first line of the input contains an integer T denoting the number of test cases.

The first and the only line of each test case contains an integer 'N', denoting the year.
Output Format:
For each test case, return “Yes” if the input year is a leap year otherwise “No”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1000 <= N <= 9999

Time limit: 1 sec

Approaches

01 Approach

  • We can make the following observations regarding every leap year:
  • If the year is divisible by 400 then it is a leap year. For example, 2000 was a leap year and it is divisible by 400.
  • If the year is not divisible by 400 and 100 but is divisible by 4 then also it is a leap year.  For example, 2020 is a leap year as it is not divisible by 400 and 100 but is divisible 4.
  • Let's take another example: the year 1000 was not a leap year because it is not divisible by 400 but it is divisible by 100.
  • So, using the above observations we can check if the year is divisible by 400 or if it is not divisible by 100 but is divisible by 4 then we return true(i.e. the input year is a leap year) otherwise, we return false(i.e. the input year is not a leap year).