Introduction:
A leap year is a year on the Gregorian calendar that has an extra day, February 29, making it 366 days long instead of the typical 365. A leap year is added to the calendar every four years to maintain synchronisation with the Earth's orbit around the Sun. In this article, we'll look at how to use the C++ programming language to check if a particular year is a leap year.
- A year can be a leap year if the year is divisible by 4.
- In case of the years having two zeros at the end if it is also divisible by 400, it's a leap year and if not then it is not a leap year.
- As the Earth's orbit around the Sun is not exactly 365 days in length, that is why we have made these rules to keep in track. By adding an extra day every four years, the calendar aligns more closely with the actual time it takes for the Earth to complete one orbit.
- To check whether a year is a leap year or not, we can follow a simple set of rules:
- If a year is divisible by 4, it could be a leap year.
- However, a year that is divisible by 100is not a leap year, unless it is also divisible by 400. Then, it is a leap year.
Checking Leap Years in C++:
With these guidelines in consideration, we are ready to craft a C++ script for identifying leap years.
C++ Program:
#include <iostream>
bool isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
int main() {
int year;
std::cout << "Enter the year: ";
std::cin >> year;
if (isLeapYear(year)) {
std::cout << " The " << year << " is a leap year.";
} else {
std::cout << " The " << year << " is not a leap year.";
}
return 0;
}
Explanation:
- A boolean result indicating whether the year is a leap year or not is returned by the isLeapYear function after receiving an integer year as input. The rules we previously established are followed by the logic.
- In the variable year, in the primary function. Then, using the supplied year as an input, we call the isLeapYear function.
- If the isLeapYear function returns true, a message informing the user that the current year is a leap year is printed. If not, a notice is printed indicating that the year is not a leap year.
Let's explore how this software operates with a few sample scenarios:
Example 1:
Suppose the program is executed and the input year is 2020.
Output 1:
Enter a year: 2020
2020 is a leap year.
2020 meets the conditions to qualify as a leap year since it is evenly divisible by 4 but not by 100.
Example 2:
Now, let's try the program with the year 1900.
Output 2:
Enter a year: 1900
1900 is not a leap year.
In this case, the year 1900 is divisible by both 4 and 100. However, it does not qualify as a leap year since it is not divisible by 400.
Example 3:
Finally, let's evaluate the program using the year 2000.
Output 3:
Enter a year: 2000
2000 is a leap year.
The year 2000 is considered a leap year as it is divisible by 4, 100, and 400.
Conclusion:
In this article, we explored utilizing the C++ programming language to verify whether a specific year is a leap year. We developed a simple application that evaluates the criteria for recognizing leap years based on the guidelines set by the Gregorian calendar.
In summary, having the ability to determine leap years in C++ is valuable for managing dates, scheduling events, and working with various time-related functions. The provided code presents a basic structure that can be expanded and modified to suit your specific needs.