Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

year
time

How to check if a year is a leap year

Educative Answers Team

A leap year is a year that has 29 days in the month of February. This is done to keep the calendar year the same as the astronomical year because astronomical years have a quarter of a day more than the calendar year. This is why a leap year occurs once every 4 years.

To see if a given year is a leap year, use the algorithm below:

If (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

Code

#include <iostream>
using namespace std;

int main() {
  int year = 2020;

  if (year % 4 != 0)
    cout << year << " is a common year.";
  else if (year % 100 != 0)
    cout << year << " is a leap year.";
  else if (year % 400 != 0)
    cout << year << " is a common year.";
  else
    cout << year << " is a leap year.";
}

RELATED TAGS

year
time
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring