A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:
#include <iostream>
int main()
{
int year;
std::cout << "Enter a year: ";
std::cin >> year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
std::cout << year << " is a leap year.\n";
else
std::cout << year << " is not a leap year.\n";
}
else
std::cout << year << " is a leap year.\n";
}
else
std::cout << year << " is not a leap year.\n";
system("pause");
return 0;
}
To check if a year is a leap year, divide the year by 4. If it is fully divisible by 4, it is a leap year. For example, the year 2016 is divisible 4, so it is a leap year, whereas, 2015 is not.
However, Century years like 300, 700, 1900, 2000 need to be divided by 400 to check whether they are leap years or not.
Comments
Leave a comment