In: Computer Science
Sun Mon
Tue Wed
Thu
Fri Sat
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
#include <iostream>
#include <iomanip>
using namespace std;
// Function that returns the index of the day of the date
// based on the parameter day - month - and year
int getDayNumberIndex(int day, int month, int year)
{
// Declares an array for term
static int term[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
year -= month < 3;
// Checks for leap year and calculates the index and returns
return ( year + year / 4 - year / 100 + year / 400 + term[month -
1] + day) % 7;
}// End of function
// Function that returns the name of the month based on the
parameter month
string getMonthName(int month)
{
// Creates an array to store month names
string monthNames[] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"
};// End of initialization
return (monthNames[month]);
}
// Function to return the number of days in a month
// from give parameter month and year
int getNumberOfDays(int month, int year)
{
// Checks if month is January then return 31 days
if (month == 0)
return (31);
// Checks if month is February then checks for leap year
// and return days based on leap and non leap year
if (month == 1)
{
// Checks if the year is leap then return 29 days
if (year % 400 == 0 || (year % 4 == 0 && year % 100 !=
0))
return (29);
// Otherwise return 28 days
else
return (28);
}// End of if condition
// Checks if month is March then return 31 days
if (month == 2)
return (31);
// Checks if month is April then return 30 days
if (month == 3)
return (30);
// Checks if month is May then return 31 days
if (month == 4)
return (31);
// Checks if month is June then return 30 days
if (month == 5)
return (30);
// Checks if month is July then return 31 days
if (month == 6)
return (31);
// Checks if month is August then return 31 days
if (month == 7)
return (31);
// Checks if month is September then return 30 days
if (month == 8)
return (30);
// Checks if month is October then return 31 days
if (month == 9)
return (31);
// Checks if month is November then return 30 days
if (month == 10)
return (30);
// Checks if month is December then return 31 days
if (month == 11)
return (31);
}// End of function
// Function to print the calendar of the given year
void printCalendar(int year, int month)
{
// Displays month name and year
cout<<endl<<endl<<setw(20)<<getMonthName
(month-1).c_str()<<" "<<year<<endl;
// To store number of days
int numberOfDays;
// Calls the function to get the index of the day from 0 to
6
int dayPosition = getDayNumberIndex(1, month, year);
// Calls the function to get number of days for the given month and
year
numberOfDays = getNumberOfDays(month - 1, year);
// Displays the day names
cout<<setw(5)<<"SUN"<<setw(5)<<"MON"<<setw(5)<<"TUE"<<setw(5)<<"WED"<<setw(5)<<"THU"
<<setw(5)<<"FRI"<<setw(5)<<"SAT"<<endl;
// Loops variable
int dayCounter;
// Loops to give space for the first date
for (dayCounter = 0; dayCounter < dayPosition;
dayCounter++)
cout<<" ";
// Loops for number of days of the given parameter month and
year
for (int day = 1; day <= numberOfDays; day++)
{
// Sets the display width of each day to 5 and displays the day
number
cout<<setw(5)<<day;
// Increase the counter by one and checks if it is greeter than
6
// (for 7 days of a week)
if (++dayCounter > 6)
{
// Resets the counter to 0
dayCounter = 0;
// Move to next line
cout<<endl;
}// End of if condition
}// End of for loop
}// End of function
// main function definition
int main()
{
// To store year and month number
int yearNumber, monthNumber;
// Accepts the year and month number
cout<<"\n Enter the year: ";
cin>>yearNumber;
cout<<"\n enter the month: ";
cin>>monthNumber;
// Calls the function to display calendar
printCalendar(yearNumber, monthNumber);
return 0;
}// End of main function
Sample Output: