Count of Leap Years in a given year range

In this article, you learn to find the count of leap years in a given year range. That means if S and E are two given years your task is to find the total number of leap years possible in the range (S, E).

I assume you already know what leap year is and how to check the leap year. But if you don’t have an idea about the leap year or are still have confusion. You can check the article on how to check leap year in C.

Consider the below example,

Input Years: 
S = 1, E = 50

Where, 
S -> Start Year
E -> End Year
 
Output: 12

 

Leap year follows the following conditions:

  • The year is a multiple of 4 but not a multiple of 100.
  • Or, The year is a multiple of 400.

 

Few approaches to Count Leap Years in a given year range:

There are many ways to count the leap year. But here I am discussing two ways. I believe after learning both approaches you are able to write the code to count leap years in your style.

Approach-1:

In this approach, I will create a simple function to check the leap year (isLeapYear()). This function returns a non-zero value for the leap year.

Now you are thinking how isLeapYear() helps you to find the counts of leap year. Don’t worry I let you know. You just take a counter variable with zero value and need to increment its value for each non-zero return of isLeapYear(). See the below code.

#include <stdio.h>


//function to find leap year
int isLeapYear(unsigned int year)
{
    int ret = ((year%400) != 0);
    ret = ret? (((year%4) == 0) && ((year%100) != 0)):1;
    return ret;
}

int main()
{
    int startYear, endYear;
    int year = 0;
    unsigned int cntLeapYear = 0;

    printf("Enter the Valid Beginning year of the range: ");
    scanf("%d",&startYear);
    printf("Enter the Valid Ending year of the range: ");
    scanf("%d",&endYear);
    /*
       Now find the count of leap year between
       the starting and ending year.
    */
    printf("\nLeap Years between %d and %d are: \n\n", startYear, endYear);
    for (year = (startYear+1); year < endYear; ++year)
    {
        const int leapYearFound = isLeapYear(year);
        if (leapYearFound)
        {
            ++cntLeapYear;
        }
    }

    printf("Total Count of Leap year = %u\n\n", cntLeapYear);
    return 0;
}

Output-1:

Enter the Valid Beginning year of the range: 1
Enter the Valid Ending year of the range: 50

Leap Years between 1 and 50 are:

Total Count of Leap year = 12

 

Approach-2:

I believe this approach is best because using this analytic approach you can avoid iteration for each year. A year is a leap year if can be divided by 4 and 400, but can’t be divided by 100. Assuming that you can count the number of leaps in the given range.

#include <stdio.h>

/*
  Function to calculate the number
  of leap years in range of (start, end)
*/
int LeapYearsBetween(int startYear, int endYear)
{
    int cntLeapYr = 0;
    if( (startYear >0) && (startYear < endYear))
    {
        cntLeapYr = (LeapYearsBefore(endYear) - LeapYearsBefore(startYear+1));
    }
    return cntLeapYr;
}

int LeapYearsBefore(int year)
{
    year--;
    return (year / 4) - (year / 100) + (year / 400);
}


int main()
{
    int startYear, endYear;
    int year = 0;

    printf("Enter the Valid Beginning year of the range: ");
    scanf("%d",&startYear);

    printf("Enter the Valid Ending year of the range: ");
    scanf("%d",&endYear);

    /*
       Now find the leap year between
       the starting and ending year.
    */
    printf("Leap Years between %d and %d are: \n", startYear, endYear);

    const int leapYearCnt = LeapYearsBetween(startYear, endYear);

    printf("Total Count of Leap year = %u\n\n", leapYearCnt);

    return 0;
}

 

Recommended Post: