please i need help . i have completed writing this code below , butmy problem is
ID: 3613685 • Letter: P
Question
please i need help . i have completed writing this code below , butmy problem is i don't understand much how the days are generatedfrom this statement firstdays = (firstdays+ numofdaysindamonth) % 7;which is inthe last part of the code in the second switch statement .pleasecan any one help me to understand how the days are generated fromeach month. the code is a completecode. this is the code#include <iostream>
using namespace std;
int main()
{
int year,months,numofdaysindamonth,firstdays;
cout <<"Enter the year::";
cin >> year;
cout <<" ";
cout <<"Enter the first day status::";
cin >> firstdays;
cout <<" ";
for(int months = 1; months <= 12; months++)
{
switch(months)
{
case 1 :
cout <<"janruary 1 ," << year<< " is ";
numofdaysindamonth = 31;
break;
case 2 :
cout <<"february 1," << year <<" is " ;
if(year % 4 == 0 && year % 100 != 0 || year %100 ==0)
numofdaysindamonth = 29;
else
numofdaysindamonth = 28;
break;
case 3 :
cout <<"March 1, " << year <<" is ";
numofdaysindamonth = 31;
break;
case 4 :
cout <<"April 1 ," << year <<" is ";
numofdaysindamonth = 30;
break;
case 5 :
cout <<"May 1 ," << year << " is ";
numofdaysindamonth = 31;
break;
case 6 :
cout <<"June 1 ," << year <<" is ";
numofdaysindamonth = 30;
break;
case 7 :
cout <<"July 1 ," << year <<" is ";
numofdaysindamonth = 31;
break;
case 8 :
cout <<"August 1 ," << year <<" is ";
numofdaysindamonth = 31;
break;
case 9 :
cout <<"September 1 ," << year<< " is ";
numofdaysindamonth = 30;
break;
case 10 :
cout <<"October 1 ," << year<< " is ";
numofdaysindamonth = 31;
break;
case 11 :
cout <<"Novemebr 1 ," << year <<" is ";
numofdaysindamonth = 30;
break;
case 12 :
cout <<"December 1 ," << year<< " is ";
numofdaysindamonth = 31;
break;
}
switch(firstdays)
{
case 0 : cout <<" Saturday " <<" ";
break;
case 1 : cout <<" Sunday " <<" ";
break;
case 2 : cout <<" Monday" <<" ";
break;
case 3 : cout <<" Tuesday " <<" ";
break;
case 4 :cout <<" Wednesday " <<" ";
break;
case 5 :cout <<" Thursday " <<" ";
break;
case 6 :cout <<" Friday " <<" ";
break;
}
firstdays = (firstdays + numofdaysindamonth) % 7;
}
system("pause");
return 0;
}
Explanation / Answer
The calculation for the first day of the next month isrepresented by
firstdays = (firstdays + numofdaysindamonth) % 7;
What this does is adds the numeric representation of the firstday to the total number of days in the month, takes the modulus of7 (divide by 7 and take the remainder), and assigns this value asthe new first day. For example, if the current month is Januarywhich has 31 days, and the first day of the month is Tuesday (valueof 3), considering the following statement:
firstdays = (3 + 31) % 7 = 34 % 7 = 6
Thus, the new first day (the first day of February) will bethe day corresponding to 6, which is Friday.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.