Answer to Question #294137 in C++ for Stephen

Question #294137

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:


1
Expert's answer
2022-02-05T06:01:22-0500
#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.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog