Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

CPP Program To Check Whether A Year Is A Leap Year Or Not

Last Updated on May 5, 2023 by Prepbytes

Every four years, an extra day is added to the year i.e., February 29, to synchronize the calendar year with the solar year. That year is known to be the leap year. Checking for a leap year is crucial in C++ programming for many applications that deal with dates and timers. A number of requirements, including divisibility by 4, 100, and 400, as well as particular guidelines for various calendar systems, are used to determine if a year is a leap year. By using if-else statements or other logical constructions, it is possible to implement the checking for leap years. A vital part of time calculation that is essential for many programs and applications is the accurate determination of leap years.

How to Check whether a Year is a Leap Year or Not in C++?

Simply enter a few mathematical conditions with the help of the If statement in the program code to determine whether a year is a leap year or not to find a leap year program in C++. In order to determine whether a certain year is a leap year or not, any of the following conditions must be satisfied:

  • The year entered must be divided by 4, but not by 100.
  • The entered year must divide by 400 equally.

Use the second criterion to distinguish between leap years and century years. Century years are those with a year ending in ’00; examples are 1300, 1500, 1400, and so forth. A leap year is only recognized when a century year is evenly divisible by 400. For instance, the years 1200, 1600, and 2000 are all century leap years since they are all exactly split by 400. We may quickly locate the leap year program in C++ by using the above technique.

Flowchart for a Leap Year Program in C++

Below is the step-by-step work of the leap year program in C++.

Algorithm for Leap Year Program in C++

Here, is the algorithm for the leap year program in C++:

  1. Declare and initialize the variable year.
  2. Go to step 3 if the year is divisible by 4. If not, proceed to step 7.
  3. Go to step 4 if the year is divisible by 100. If not, proceed to step 5.
  4. Go to step 5 if the year is divisible by 400. If not, proceed to step 7.
  5. It’s a leap year this year.
  6. Publish a message stating that it is a leap year in the current year.
  7. It’s not a leap year this year.
  8. Declare that the year is not a leap year by printing a statement.

Pseudo Code to Check whether a Year is a Leap Year or Not in C++

Given below is the pseudo-code for the leap year program in C++:

if (year is not divisible by 4) then it is not a leap 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 not a year
else it is a leap year

Program to Check whether a Year is a Leap Year or Not in C++

In the leap year program in C++ example, the user should enter a year and determine whether or not it was a leap year. But in this case, we’ll create a leap year program in C++ that will prompt the user for a range and print out all the leap years that fall within that range.

Code Implementation

#include <iostream>
using namespace std;

int main()
{
    int start_Year, end_Year, i;
    cout<<"Enter the starting year of the range: ";
    cin>>start_Year;
    cout<<"Enter the last year of the range: ";
    cin>>end_Year;
    cout<<"Leap Years between"<<start_Year<<" and "<<end_Year<<" are: \n";
    for (i= start_Year; i<= end_Year; i++)
{
        if (((i % 4 == 0) && (i % 100!= 0)) || (i % 400 == 0))
{
            cout<<i<<endl;
        }
    }
    return 0;
}

Input

2000
2023

Output

2000
2004
2008
2012
2016
2020

Conclusion
We covered how to check whether a year is a leap year or not and explained the approach with a flowchart, algorithm, and logic. But surely, there are numerous ways to check whether a year is a leap year or not.

Frequently Asked Questions(FAQs):

Q1. Why is it necessary for the C++ leap year program to be checked?
Ans: Since they have an impact on the calculation of time intervals and the number of days in a year, the leap year program in C++ is crucial to take into account in programs that deal with dates and times. For instance, you must take into consideration the fact that some years have an extra day while developing a program that computes interest on a loan that is spread over a number of years.

Q2. Is it possible to calculate the leap year in C++ using simply if-else statements?
Ans: Yes, as demonstrated in the example pseudo-code above, it is possible to implement the logic for determining the leap year using if-else statements. The logic can also be implemented in different ways, as with a switch statement or ternary operator.

Q3. Does the C++ programming language include a built-in function to check for leap years?
Ans: No, there is no built-in function in C++ to check for leap year. You can use libraries and third-party functions for this purpose, though.

Leave a Reply

Your email address will not be published. Required fields are marked *