# Check if a given date is weekend or not in C++

In this tutorial, we will learn how to check if a given date is a weekend or not in C++.

We will first define a function that will calculate the day of the week using the given date, month, and year.

## Define a function to find the day of the week for a given date in C++

Algorithm for the Function:

Here we will use the magic month array that will help us in determining the day of the week.

`{ 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}`

The 12 numbers in the array represent each of the 12 months of a year.

The Day array will start from 0 to 6 :

`{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}`

Lastly, we will use the Century Year Value as follows:

1700-1799 = 4
1800-1899 = 2
1900-1999 = 0
2000-2099 = 6

For this example, we will determine the day of the week of 18/12/2021

Step1: Take the last two digits of the year:   21

Step2: Divide the digit with 4:   21/4 = 6

Step3: Take the date:   18

Step4: Take the month value from the magic month array:   4  (4 is for December month number 12)

Step4: Take Century year value of the given year:   6   (as 2021 lies in between 2000-2099)

Step5: Sum all the values and divide the sum by 7.

21+6+18+4+6 = 55

55%7 = 6  (By dividing 55 by 7 we got the reminder 6)

By checking the Day array, have got the result: Day[6] = Saturday

Let’s define the function in C++:

```int DayOfWeek(int date, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3,
5, 1, 4, 6, 2, 4 }; //magic month array

y -= m < 3; //to get the last 2 digit of the year

return ( y + y / 4 - y / 100 +
y / 400 + t[m - 1] + d) % 7;
}```

Now, let’s go to our main program, which is to check whether a given date is a weekend or not using this DayOfWeek() function.

## Program to check if a date is a weekend or not in C++

Here in this program, Sunday and Saturday have taken as the weekend.

```#include <iostream>
using namespace std;

int dayofweek(int date, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3,
5, 1, 4, 6, 2, 4 };
year -= month < 3;
return ( year + year / 4 - year / 100 +
year / 400 + t[month - 1] + date) % 7;
}

void checkWeekday(int day) {

if(day == 0 || day == 6) {   //Sunday(0) and Saturday(6)

cout << "This is a Weekend !!!";
}
else {
cout << "This is not a Weekend.";
}
}

int main()
{
int year,month,date;

cout << "Enter The Date To Check:" << endl;
cout << "Year: ";
cin >> year;

cout << "Month: ";
cin >> month;

cout << "Date: ";
cin >> date;

cout << "You Have Entered: " << date << "/" << month << "/" << year << endl;
int day = dayofweek(date, month, year);
checkWeekday(day);
return 0;
}```

Here we have defined another function, checkWeekday() to check and print whether the day is a Weekend(Sunday or Saturday).

Output1:

```Enter The Date To Check:
Year: 2021
Month: 11
Date: 23
You Have Entered: 23/11/2021
This is not a Weekend.```

Output2:

```Enter The Date To Check:
Year: 2021
Month: 12
Date: 18
You Have Entered: 18/12/2021
This is a Weekend !!!.```

Hope you have enjoyed this article and learned how we can check whether a given date is a weekend or not in C++.

Happy Coding!!

You can also read, Check if a date is a future date or not in C++