To be completed in C++: Monthly Calender Program Write a program that will displ
ID: 3678279 • Letter: T
Question
To be completed in C++: Monthly Calender Program
Write a program that will display a calendar for any given month of a given year. The user will need to type the number of the month as an integer from 1 to 12 (1 is for January, etc.), and the year as a 4-digit integer.
To determine the 1st day of any given month for any given year; Calculated it as an integer D, from 0 to 6 (0 for Sunday, 1 for Monday, … and 6 for Saturday), using the following formula:
D = ([26 *(M +1) / 10]+Y+[Y/4]+[C/4]+5*C)mod 7
where:
• M is the month (3 is for March, 4 is for April, . . ., 12 is for December, 13 is for January, and 14 is for February);
• C is the "century" (more accurately, the first 2 digits of the year);
• Y is the year within the "century" (i.e., the last 2 digits of the year).
Requirements:
This assignment simply requires that your program make use of more than one loop statements and as many functions as possible to breakdown the problem into simpler sub-problems. Also, you must have at least one valued function and one void function. In addition, just as Nickie's program, your program should keep the calendar neatly tabulated.
Any help would be appreciated. Thanks.
Explanation / Answer
#include<iostream>
using namespace std;
string getMonthName(int month){
string monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
return monthString;
}
string getDayName(int day){
string dayString = "Monday";
switch(day)
{
case 1:
dayString= "Monday";
break;
case 2:
dayString ="Tuesday";
break;
case 3:
dayString ="Wednesday";
break;
case 4:
dayString ="Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 0:
dayString ="Sunday";
}
return dayString;
}
int getCentuary(int year){
return year/100;
}
int getYear(int year){
return year%100;
}
int getDayNumber(int M, int year){
int C = getCentuary(year);
int Y = getYear(year);
int D = ((26 *(M +1) / 10)+Y+(Y/4)+(C/4)+5*C)% 7;
return D;
}
int main(){
int month;
int year;
cout<<"Enter month: ";
cin>>month;
cout<<"Enter year: ";
cin>>year;
// getting day number
int day = getDayNumber(month, year);
string monthName = getMonthName(month);
string dayName = getDayName(day);
cout<<"First day of month "<<monthName<<" and "<<year<<" is "<<dayName<<endl;
return 0;
}
/*
Output:
Enter month: 12
Enter year: 1994
First day of month December and 1994 is Thursday
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.