1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <iostream> #include <iomanip> using namespace std; // Function to check if the year is a leap year bool isLeapYear(int year) { if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) return true; else return false; } else return true; } else return false; } // Function to get the number of days in a month int getDaysInMonth(int month, int year) { switch (month) { case 1: return 31; // January case 2: return isLeapYear(year) ? 29 : 28; // February case 3: return 31; // March case 4: return 30; // April case 5: return 31; // May case 6: return 30; // June case 7: return 31; // July case 8: return 31; // August case 9: return 30; // September case 10: return 31; // October case 11: return 30; // November case 12: return 31; // December default: return 0; } } // Function to calculate the day of the week for the 1st of a month int getStartDay(int month, int year) { int day = 1; // First day of the month int century = year / 100; int yearInCentury = year % 100; if (month == 1 || month == 2) { month += 12; yearInCentury--; } int dayOfWeek = (day + (13 * (month + 1)) / 5 + yearInCentury + yearInCentury / 4 + century / 4 + 5 * century) % 7; return (dayOfWeek + 5) % 7; // Adjusting to start from Monday } // Function to display the calendar for a given month and year void displayCalendar(int month, int year) { string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; cout << "\n " << months[month - 1] << " " << year << "\n"; cout << " Mo Tu We Th Fr Sa Su\n"; int daysInMonth = getDaysInMonth(month, year); int startDay = getStartDay(month, year); // Print leading spaces for the first week for (int i = 0; i < startDay; i++) cout << " "; for (int day = 1; day <= daysInMonth; day++) { cout << setw(2) << day << " "; if ((day + startDay) % 7 == 0) cout << "\n"; // New line after Sunday } cout << "\n"; } int main() { int month, year; cout << "Enter month (1-12): "; cin >> month; cout << "Enter year: "; cin >> year; displayCalendar(month, year); return 0; } |
Explanation:
- Leap Year Calculation:
- The function
isLeapYear
checks whether a given year is a leap year. A leap year occurs every 4 years, but years divisible by 100 are not leap years unless they are also divisible by 400.
Advertisement - The function
- Days in a Month:
- The function
getDaysInMonth
returns the number of days in a particular month, considering if it’s February in a leap year.
- The function
- Start Day Calculation:
- The function
getStartDay
calculates the day of the week for the 1st of a given month using Zeller’s Congruence, adjusted to start from Monday.
- The function
- Calendar Display:
- The function
displayCalendar
prints the calendar for the input month and year. It starts by printing the days of the week, then aligns the dates according to the starting day of the month.
Advertisement - The function
- Main Function:
- The
main
function takes user input for the month and year and calls thedisplayCalendar
function to show the calendar.
- The
Possible Enhancements:
- Event Scheduling: You could extend this program by allowing users to schedule events on specific dates.
- Navigation: Adding functionality to move between months and years interactively.
- Graphical Interface: Implementing a graphical interface for easier interaction.