C++
#include <iostream>
#include <string>
using namespace std;
string find_day(int date, int month, int year) {
string days[] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (isLeap) month_days[1] = 29;
int total_days = 0;
total_days += (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
for (int i = 0; i < month - 1; i++) {
total_days += month_days[i];
}
total_days += date;
return days[total_days % 7];
}
int main() {
cout << find_day(15, 8, 1947) << endl;
cout << find_day(26, 1, 1950) << endl;
return 0;
}
Java
public class FindDay {
public static String find_day(int date, int month, int year) {
String[] days = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
int[] month_days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (isLeap) month_days[1] = 29;
int total_days = 0;
total_days += (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
for (int i = 0; i < month - 1; i++) {
total_days += month_days[i];
}
total_days += date;
return days[total_days % 7];
}
public static void main(String[] args) {
System.out.println(find_day(15, 8, 1947));
System.out.println(find_day(26, 1, 1950));
}
}
Python
def find_day(date, month, year):
days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
if is_leap:
month_days[1] = 29
total_days = 0
total_days += (year - 1) * 365 + (year - 1)
total_days += sum(month_days[:month - 1])
total_days += date
return days[total_days % 7]
print(find_day(15, 8, 1947))
print(find_day(26, 1, 1950))
Explanation of Logic:
- Odd Days Concept: The day of the week repeats in cycles of 7.
- Leap Year Handling: Leap years have an extra day in February.
- Steps:
- Count days for complete years up to year - 1.
- Add days for complete months in the given year.
- Add the given date.
- Use modulo 7 to find the day of the week.
Example Outputs:
For find_day(15, 8, 1947)
the output is "friday".
For find_day(26, 1, 1950)
the output is "thursday".