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

In C++, Write a program that, given a date, three ints (for example, 11 27 1997)

ID: 3583917 • Letter: I

Question

In C++, Write a program that, given a date, three ints (for example, 11 27 1997), will print the number of that day within its year: i.e. Jan 1st is always 1, Dec 31st is either 365 or 366.

The months of the year have lengths according to the following rules:

The odd months up to and including month 7 have 31 days.

The even months from 8 upwards, have 31 days.

Month 2 has 28 days except in a leap year when it has 29 days.

The rest of the months have 30 days.

Write the program as a function and write a main() to test it. Please include explanations of every step in comments.

Explanation / Answer

#include <iostream>
using namespace std;

int main(){
   int date,month,year;
   cout<<"Plese enter Month Day Year, all input is in integer"<<endl;
   cin>>month>>date>>year;
   int day=0;

   for(int i=1; i<month; i++){
       if(i==2){
           if(year%4==0){       //checking if a year is leap year by taking mod by 4
               day+=29;       //increase day by 29
           }
           else{
               day+=28;
           }
       }
       else if((i%2==0 && i<7) || (i%2!=0 && i>7)){       // (condition 1 AND condition 2) OR (condition 2 AND condition 3)
           day+=30;
       }
       else{
           day+=31;
       }
   }
   day+=date;       //add date to day
   cout<<"The day is: "<<day<<endl;
   return 0;
}