The Program in C to Check for weekday with Given Date,Month,Year are given below:
#include <stdio.h>
int dayOfWeek(int d, int m, int y) {
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
int main() {
int day, month, year, result;
printf("Enter a date (dd mm yyyy): ");
scanf("%d %d %d", &day, &month, &year);
result = dayOfWeek(day, month, year);
switch(result) {
case 0:
printf("Sunday\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
default:
printf("Invalid input\n");
}
return 0;
}
Output:
Enter a date (dd mm yyyy): 08
08
1999
Sunday
Pro-Tips💡
This program prompts the user to enter a date in the format “dd mm yyyy” (day, month, year).
Then it calls a function dayOfWeek(int d, int m, int y)
which uses the Zeller’s congruence algorithm to calculate the day of the week from the given date.
The function return an integer, which is the corresponding day of the week (Sunday = 0, Monday = 1, …, Saturday = 6).
Then it uses a switch statement to print the name of the day of the week based on the result returned by the function.
Note that this algorithm is accurate for dates between the late 1800s and 2100, if you need to handle dates beyond that range, you might need to use other algorithms
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.