#include <stdio.h>
int main() {
int year, month;
// Input year and month
printf("Enter year: ");
scanf("%d", &year);
printf("Enter month (1-12): ");
scanf("%d", &month);
// Calculate the number of days in the given month and year
int daysInMonth;
if (month == 2) {
// Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
daysInMonth = 29;
else
daysInMonth = 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
// Display the calendar header
printf("\n Sun Mon Tue Wed Thu Fri Sat\n");
// Calculate the day of the week for the first day of the month
int dayOfWeek = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
for (int i = 1; i < month; i++) {
if (i == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
dayOfWeek += 29;
else
dayOfWeek += 28;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
dayOfWeek += 30;
} else {
dayOfWeek += 31;
}
}
dayOfWeek = (dayOfWeek + 1) % 7;
// Display the calendar body
for (int i = 0; i < dayOfWeek; i++) {
printf(" ");
}
for (int i = 1; i <= daysInMonth; i++) {
printf("%5d", i);
if ((dayOfWeek + i) % 7 == 0 || i == daysInMonth) {
printf("\n");
}
}
return 0;
}