In: Computer Science
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day the month ended.
ex: if the user enters for year: 2020 for month: 2 for day of week that 1st month fell: 7
The outcome should look like
Here is the calendar for February of 2020
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
The month ends on a Saturday.
For the month of February, the program has to decide if the year
is leap (29 days) or not.
.
Code:
#include <iostream>
using namespace std;
int main()
{
int year,month,day,total=0;
string m[]={"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"};
string d[]={"Sunday","Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"};
cout<<"Enter year: ";
cin>>year;
cout<<"Enter month: ";
cin>>month;
cout<<"The day of the week on which the first of the month
was: ";
cin>>day;
if(month == 1 ||month == 3||month == 5||month == 7||month ==
8||month == 10||month == 12)
total = 31;
else if(month == 4||month == 6||month == 9||month == 11)
total = 30;
else
{
if (year % 400 == 0 || (year % 4 == 0 && year % 100 !=
0))
total = 29;
else
total = 28;
}
cout<<"\nHere is the calendar for
"<<m[month-1]<<" of "<<year;
cout<<"\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n";
int k,j;
for (k = 0; k < day-1; k++)
cout<<"\t";
for (j = 1; j <= total; j++)
{
cout<<j<<"\t";
if (++k > 6)
{
k = 0;
cout<<"\n";
}
}
k = k-1;
if(k<0)
k = 6;
cout<<"\nThe month ends on a "<<d[k]<<".";
return 0;
}
Please refer to the screenshot of the code to understand the indentation of the code:
Output:
For any doubts or questions comment below.