C++ program to find the last Saturday of each month in a given year

In this tutorial, we’ll learn how to find the last Saturday of each month in a given year in C++.

Let us see an example of how this program works

Enter the year: 2020

January: 25

February: 29

March: 28

April: 25

May: 30

June: 27

July: 25

August: 29

September: 26

October: 31

November: 28

December: 26

In the above example, the left column represents the months and the right column represents the date of the month in which the last Saturday falls.

How to find the last Saturday of every month of a given year in C++

In this program first, we need to enter the number of days each months contain as it differs. Then we need to check whether the given year is a leap year or not using isleapyear() function. For being the leap year it should be divided by 4 and 100. Then we make use of getWeekDay() function to know the last Saturday of the month. Then in the main function we declare the last Saturday of the month.

Now let us see the code for how to find the last Saturday of the Months in the Year.

Program:

#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
class last_Saturday
{
public:
    last_Saturday()
    {
      m[0]  = "January:   "; m[1]  = "February:  "; m[2]  = "March:     "; m[3]  = "April:     "; 
      m[4]  = "May:       "; m[5]  = "June:      "; m[6]  = "July:      "; m[7]  = "August:    "; 
      m[8]  = "September: "; m[9]  = "October:   "; m[10] = "November:  "; m[11] = "December:  "; 
    }
    void findlast_Saturday( int y )
    {
      years = y;
      isleapyear();
        int days[] = { 31, isleap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    d;
      for( int i = 0; i < 12; i++ )
      {
          d = days[i];
          while( true )
          {
            if( !getWeekDay( i, d ) ) break;
            d--;
          }
          lastDay[i] = d;
      }
        display();
    }
 
private:
    void isleapyear()
    {
      isleap = false;
      if( !( years % 4 ) )
      {
          if( years % 100 ) isleap = true;
          else if( !( years % 400 ) ) isleap = true;
      }
    }
    void display()
    {
      cout << "  YEAR " << years << endl << "=============" << endl;
      for( int x = 0; x < 12; x++ )
          cout << m[x] << lastDay[x] << endl;
        cout << endl << endl;
    }
    int getWeekDay( int m, int d )
    {
      int y = years;
        int f = y + d + 3 * m ;
      m++;
      if( m < 3 ) y--;
      else f -= int( .4 * m + 2.3 );
        f += int( y / 4 ) - int( ( y / 100 + 1 ) * 0.75 );
      f %= 7;
        return f;
    }
    int lastDay[12], years;
    string m[12];
    bool isleap;
};
int main( int argc, char* argv[] )
{
    int y;
    last_Saturday ls;
 
    while( true )
    {
      cout << "Enter the year in the form of YYYY and 0 to quit: "; 
      cin >> y;
      if( !y ) return 0;
        ls.findlast_Saturday( y );
    }
    return 0;
}

Output:

Enter the year in the form of YYYY and 0 to quit: 2021
January:    30
February:   27
March:      27
April:      24
May:        29
June:       26
July:       31
August:     28
September:  25
October:    30
November:   27
December:   25

For a better understanding of the program kindly follow the below-attached links.

How to find day from date in C++

How to print calendar for a specific year in C++

Leave a Reply

Your email address will not be published.