Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ : Write a program that prints the day number of the year, given the date in

ID: 675188 • Letter: C

Question

C++ : Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400. C++ : Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400. C++ : Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400.

Explanation / Answer

The program you required is below:

#include<iostream>

using namespace std;

void noOfDays(int, int, int);

int main()

{

int day, month, year;

cout<<"Please enter month, day and year: "<<endl<<endl; cin>>month>>day>>year; noOfDays(day, month, year); //system ("pause");

return 0; }

void noOfDays(int d, int m, int y)

{ int numDays = 0; bool leap;

if (y % 4 == 0)

{

cout<< "The year " << y <<" is a leap year. "<<endl<<endl; numDays = 366; leap = true;

}

else

{ cout<< "The year " << y <<" is not a leap year. "<<endl<< endl; numDays = 365; leap = false; }

switch (m)

{ c

ase 12:numDays -= (31-d); break;

case 11:numDays -= (61-d); break;

case 10:numDays -= (92-d); break;

case 9:numDays -= (122-d); break;

case 8:numDays -= (153-d); break;

case 7:numDays -= (184-d); break;

case 6:numDays -= (214-d); break;

case 5:numDays -= (245-d); break;

case 4:numDays -= (275-d); break;

case 3:numDays -= (306-d); break;

case 2: { if(leap) numDays -= (335-d);

else numDays -= (334-d); } break;

case 1: { if(leap) numDays -= (366-d);

else numDays -= (365-d); } break;

default: cout<<"no such month"<<endl;

}

cout<<numDays;

}