Write this code in C++ Instructions: Write a program named DayOfWeek that comput
ID: 3905544 • Letter: W
Question
Write this code in C++Instructions: Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week for that date as a number between 0 and 6, where 0 represents Saturday and 6 represents Friday. The following example shows what the user will see on the screen: This program calculates the day of the week for any date. Enter month (1-12): 9 Enter day (1-31): 25 Enter century: 19 Enter year of the century: 98 The day of the week is 6 (0-Sat, 1-Sun, 2-Mon, 3-Tue, 4-Wed, 5-Thu, 6-Fri)
Hint: Use Zeller’s congruence to compute the day of the week. Use all integers for the data types. Zeller’s congruence relies on the following quantities: j is the century (19 in our example) k is the year within the century (98) m is the month (9) q is the day of the month (25)
The day of the week is determined by the following formula:
h = (q + 26(m + 1)/10 + k + k/4 + j/4 + 5j)mod 7
The results of all divisions are truncated by the modulus operator. The value of h will lie between 0 (Saturday) and 6 (Friday). Note: Zeller’s congruence assumes that January and February are treated as months 13 and 14 of the previous year; this affects the values of k and m, and possibly the value of j. For full credit, just read in the four values (month, day, century, and year), plug them into the formula, and print h, the day of the week. This formula assumes that January and February are months 13 and 14 of the previous year. If you want the day of week for 1 1 2006, you must type in 13 1 2005. (For 2 1 2006, type 14 1 2005 and so on)
Run:
Run the following 8 sets of data to fully test your program: 5 17 2008 (Should be 0-Sat) 10 5 2008 (Should be 1-Sun) 9 15 2008 (Should be 2-Mon) 8 5 2008 (Should be 3-Tue) 9 13 2006 (Should be 4-Wed) 1 1 2009 (Should be 5-Thu) must be typed in as 13 1 2008 2 6 2009 (Should be 6-Fri) must be typed in as 14 6 2008 1 10 2000 (Should be 2-Mon) must be typed in as 13 10 1999 Your birthday Extra: Use an if statement, or a switch statement, to print the actual word for the day of the week, ie. Saturday, Sunday, Monday, Tuesday, etc. and adjusting the month of January and February so you don’t have to type in month 13 and 14 with the earlier year. Also you must consider a January date in 2000 that has to be set back to 1999. Use the original data as given above. The last three should be entered as: 1 1 2009 2 6 2009 1 10 2000
Write this code in C++
Instructions: Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week for that date as a number between 0 and 6, where 0 represents Saturday and 6 represents Friday. The following example shows what the user will see on the screen: This program calculates the day of the week for any date. Enter month (1-12): 9 Enter day (1-31): 25 Enter century: 19 Enter year of the century: 98 The day of the week is 6 (0-Sat, 1-Sun, 2-Mon, 3-Tue, 4-Wed, 5-Thu, 6-Fri)
Hint: Use Zeller’s congruence to compute the day of the week. Use all integers for the data types. Zeller’s congruence relies on the following quantities: j is the century (19 in our example) k is the year within the century (98) m is the month (9) q is the day of the month (25)
The day of the week is determined by the following formula:
h = (q + 26(m + 1)/10 + k + k/4 + j/4 + 5j)mod 7
The results of all divisions are truncated by the modulus operator. The value of h will lie between 0 (Saturday) and 6 (Friday). Note: Zeller’s congruence assumes that January and February are treated as months 13 and 14 of the previous year; this affects the values of k and m, and possibly the value of j. For full credit, just read in the four values (month, day, century, and year), plug them into the formula, and print h, the day of the week. This formula assumes that January and February are months 13 and 14 of the previous year. If you want the day of week for 1 1 2006, you must type in 13 1 2005. (For 2 1 2006, type 14 1 2005 and so on)
Run:
Run the following 8 sets of data to fully test your program: 5 17 2008 (Should be 0-Sat) 10 5 2008 (Should be 1-Sun) 9 15 2008 (Should be 2-Mon) 8 5 2008 (Should be 3-Tue) 9 13 2006 (Should be 4-Wed) 1 1 2009 (Should be 5-Thu) must be typed in as 13 1 2008 2 6 2009 (Should be 6-Fri) must be typed in as 14 6 2008 1 10 2000 (Should be 2-Mon) must be typed in as 13 10 1999 Your birthday Extra: Use an if statement, or a switch statement, to print the actual word for the day of the week, ie. Saturday, Sunday, Monday, Tuesday, etc. and adjusting the month of January and February so you don’t have to type in month 13 and 14 with the earlier year. Also you must consider a January date in 2000 that has to be set back to 1999. Use the original data as given above. The last three should be entered as: 1 1 2009 2 6 2009 1 10 2000
Write this code in C++
Instructions: Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week for that date as a number between 0 and 6, where 0 represents Saturday and 6 represents Friday. The following example shows what the user will see on the screen: This program calculates the day of the week for any date. Enter month (1-12): 9 Enter day (1-31): 25 Enter century: 19 Enter year of the century: 98 The day of the week is 6 (0-Sat, 1-Sun, 2-Mon, 3-Tue, 4-Wed, 5-Thu, 6-Fri)
Hint: Use Zeller’s congruence to compute the day of the week. Use all integers for the data types. Zeller’s congruence relies on the following quantities: j is the century (19 in our example) k is the year within the century (98) m is the month (9) q is the day of the month (25)
The day of the week is determined by the following formula:
h = (q + 26(m + 1)/10 + k + k/4 + j/4 + 5j)mod 7
The results of all divisions are truncated by the modulus operator. The value of h will lie between 0 (Saturday) and 6 (Friday). Note: Zeller’s congruence assumes that January and February are treated as months 13 and 14 of the previous year; this affects the values of k and m, and possibly the value of j. For full credit, just read in the four values (month, day, century, and year), plug them into the formula, and print h, the day of the week. This formula assumes that January and February are months 13 and 14 of the previous year. If you want the day of week for 1 1 2006, you must type in 13 1 2005. (For 2 1 2006, type 14 1 2005 and so on)
Run:
Run the following 8 sets of data to fully test your program: 5 17 2008 (Should be 0-Sat) 10 5 2008 (Should be 1-Sun) 9 15 2008 (Should be 2-Mon) 8 5 2008 (Should be 3-Tue) 9 13 2006 (Should be 4-Wed) 1 1 2009 (Should be 5-Thu) must be typed in as 13 1 2008 2 6 2009 (Should be 6-Fri) must be typed in as 14 6 2008 1 10 2000 (Should be 2-Mon) must be typed in as 13 10 1999 Your birthday Extra: Use an if statement, or a switch statement, to print the actual word for the day of the week, ie. Saturday, Sunday, Monday, Tuesday, etc. and adjusting the month of January and February so you don’t have to type in month 13 and 14 with the earlier year. Also you must consider a January date in 2000 that has to be set back to 1999. Use the original data as given above. The last three should be entered as: 1 1 2009 2 6 2009 1 10 2000
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int month,day,year,century; //variables declaration
string weekdays[7] = {"saturday","sunday","monday","tuesday","wednesday","thursday","friday"};
cout<<"Enter month (1-12): "; //reading data from user
cin>>month;
cout<<" Enter day(1-31): ";
cin>>day;
cout<<" Enter century: ";
cin>>century;
cout<<" Enter year of century: ";
cin>>year;
int h;
//calculating by using your formula
h = (day + 26*(month + 1)/10 + year + year/4 + century/4 + 5*century) % 7;
cout <<" (0-Sat, 1-Sun, 2-Mon, 3-Tue, 4-Wed, 5-Thu, 6-Fri)";
//pringting Final result
cout<<" The day of the week is: "<<h <<"day: "<<weekdays[h];
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.