Find Leap Year in C++

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.

Checking Leap Years in C++:

  • 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.

With these rules in mind, we can proceed to write a C++ program to determine leap years.

C++ Program:

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 see how this program works with some examples:

Example 1:

Suppose we run the program and enter the year 2020.

Output 1:

Enter a year: 2020
2020 is a leap year.

2020 fits the criteria for being a leap year because it is divisible by 4 and 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 instance, 1900 is not only divisible by 4, but also by 100. It is not a leap year, though, as it is not divisible by 400.

Example 3:

Finally, let's test the program with the year 2000.

Output 3:

Enter a year: 2000
2000 is a leap year.

The year 2000 is regarded as a leap year because it may be divided by 4, 100, and 400.

Conclusion:

In this post, we looked at how to use the C++ programming language to check if a particular year is a leap year. We built a straightforward programme that examines the conditions to identify leap years in accordance with the Gregorian calendar's standards for leap years.

In conclusion, knowing how to calculate leap years in C++ is useful for handling dates, planning events, and using other time-related applications. The offered code offers a fundamental framework that may be developed and altered to meet your unique requirements.






Latest Courses