In: Computer Science
#include <iostream> #include <iomanip> using namespace std; char months[13][100] = {"", "January","February","March","April","May","June","July","August","September","October","November","December"}; char days[7][100] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; int isLeapYear(int year) { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } unsigned int dayOfWeek(int y, int m, int d) { m = (m + 9) % 12; y -= m / 10; unsigned int dn = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d - 1); return (dn + 3) % 7; } int findDaysInMonth(int year, int month) { int days = 31; if((month==6) || (month==4) || (month==11) || (month==9)) { days = 30; }else if((month==2) && isLeapYear(year)) { days = 29; } else if(month == 2) { days = 28; } return days; } int printMonthCalendar(int year, int month) { int numOfDays = findDaysInMonth(year, month); int startingDay = dayOfWeek(year, month, 1); for(int i=0; i<7; i++) { cout << setw(5) << days[i]; } cout << endl; for(int i=1; i<startingDay; i++) { cout << setw(5) << ""; } int day = 1; int weekDay = startingDay - 1; while(day <= numOfDays) { cout << setw(5) << day; day++; weekDay++; if(weekDay == 7) { cout << endl; weekDay = 0; } } cout << endl; return weekDay + 1; } void printYearCalendar(int year) { for(int month=1; month<=12; month++) { cout << months[month] << ", " << year << endl; printMonthCalendar(year, month); cout << endl; } } int main() { // to print year calendar printYearCalendar(2019); // to print month calendar cout << endl << endl << "Month calendar" << endl; printMonthCalendar(2019, 10); }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.