
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Day of the Week in C++
Suppose we have a date (day, month and year). From this date, we have to find the day of the week of that given date. To solve this we will use Zeller’s Algorithm. The formula to find weekday using Zeller’s Algorithm is here
𝑤=$$\lgroup d+\lfloor \frac{13(m+1)}{5} \rfloor+y+\lfloor\frac{y}{4} \rfloor+\lfloor\frac{c}{4} \rfloor+5c \rgroup mod 7$$
The formula is containing some variables; They are −
d − The day of the date.
m − It is the month code. For March to December, it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then the given year will be decreased by 1.
y − Last two digits of the year
c − first two digits of the year
w − The weekday. When it is 0, it is Saturday, when it is 6, it means Friday
So for example, if we want to get the weekday of 4th January 1997, then the output will be “Saturday”
The algorithm is as follows −
Algorithm
zellersAlgorithm(day, month, year)
Input − The date of the day.
Output − Which day it was, (Sunday to Saturday).
Begin if month > 2, then mon := month else mon := 12 + month decrease year by 1 y := last two digit of the year c := first two digit of the year w := day + floor((13*(mon+1))/5) + y + floor(y/4) + floor(c/4) + 5*c w := w mod 7 return weekday[w] //weekday will hold days from Saturday to Friday End
Example (C++)
#include #include using namespace std; string weekday[7] = {"Saturday","Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday"}; string zellersAlgorithm(int day, int month, int year){ int mon; if(month > 2) mon = month; //for march to december month code is same as month else{ mon = (12+month); //for Jan and Feb, month code will be 13 and 14 year--; //decrease year for month Jan and Feb } int y = year % 100; //last two digit int c = year / 100; //first two digit int w = (day + floor((13*(mon+1))/5) + y + floor(y/4) + floor(c/4) + (5*c)); w = w % 7; return weekday[w]; } int main(){ int day, month, year; cout << "Enter Day: "; cin >>day; cout << "Enter Month: "; cin >>month; cout << "Enter Year: "; cin >>year; cout << "It was: " <<zellersAlgorithm(day, month, year); }
Input
(4, 1, 1997)
Output
Enter Day: 4 Enter Month: 1 Enter Year: 1997 It was: Saturday
- Related Articles
- Get the week number in MySQL for day of week?
- Get Day Number of Week in Java
- Finding day of week from date (day, month, year) in JavaScript
- How to get the day of the week in JavaScript?
- Crontab day of the week syntax on Linux
- Formatting day of week using SimpleDateFormat in Java
- In MySQL, how we can compute date by providing the year, week number and day of the week?\nday of the week?
- Formatting day of week in EEEE format in Java
- Java Program to get day of week for the last day of each month in 2019
- Python Pandas - Get the Day of the week the period lies in
- How to get first day of week in PHP?
- Get the day of week for a particular date in Java
- Display Day Name of Week using Java Calendar
- C# Program to get current day of week
- How to get first day of the week using Python?
