Declare a 12element int array named days Assign the number o

Declare a 12-element int array named days. Assign the number of days in each month to

the array, using 28 for February. Code the program so that it displays the number of

days corresponding to the month number entered by the user. For example, when the

user enters the number 7, the program should display the number 31. However, if the

user enters the number 2, the program should ask the user for the year. The rules for

determining whether a year is a leap year are shown in Figure 11-51. If the year is a leap

year, the program will need to add 1 to the number of days before displaying the number

of days on the screen. The program should also display an appropriate message

when the user enters an invalid month number. Use a sentinel value to end the program.

Save and then run the program. Test the program using the number 1, and then test it using the

numbers 3 through 12. Test it using the number 2 and the year 2015. Then, test it using

the number 2 and the year 2016. Also test it using an invalid number, such as 20

Figure 11-51

1. If the year number is not evenly divisible by 4, it is not a leap year.

2. If the year number is evenly divisible by 4 and is not evenly divisible by 100,

then it is a leap year.

3. If the year number is evenly divisible by both 4 and 100 and is also evenly

divisible by 400, then it is a leap year; otherwise, it is not a leap year.

Solution

#include <iostream>

using namespace std;

int main()
{int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //declaring array days of 12 months
int month,year; //variable to store month and year
cout << \"Enter month \" << endl; //asking user to enter months
cin>>month; //storing user entered value in month
cout << \"Enter year\" << endl; //asking user to enter year
cin>>year; //storing user entered value in year
if(month>12 || month<1) //if entered month is invalid
{cout << \"Please enter a valid month number between 1 to 12 both inclusive \" << endl; //asking user to enter valid month
cin>>month;
if(year % 4==0 && year % 100 != 0) //leap year
{
if(month==2)
cout<<days[1]+1<<endl; //increase 1 day in february
else
cout<<days[month-1]<<endl;
}
else if((year % 4==0 && year % 100 == 0)&& year % 1000 == 0) //leap year
{
if(month==2)
cout<<days[1]+1<<endl; //increase 1 day in february
else
cout<<days[month-1]<<endl;
}
else //not a leap year
cout<<days[month-1]<<endl;
  
}
else{ //user entered month is valid
if(year % 4==0 && year % 100 != 0) //leap year
{
if(month==2)
cout<<days[1]+1<<endl; //increase 1 day in february
else
cout<<days[month-1]<<endl;
}
else if((year % 4==0 && year % 100 == 0)&& year % 1000 == 0) //leap year
{
if(month==2)
cout<<days[1]+1<<endl; //increase 1 day in february
else
cout<<days[month-1]<<endl;   
}
else //not a leap year
cout<<days[month-1]<<endl;
  
}
return 0;


}

*******OUTPUT********
Enter month   
2   
Enter year
2016
29   
*******OUTPUT********
Note:this code has been tested on gcc compiler and had been tested with all the test cases you mentioned,its working fine
Please contact in case of any doubt,Thanks.

Declare a 12-element int array named days. Assign the number of days in each month to the array, using 28 for February. Code the program so that it displays the
Declare a 12-element int array named days. Assign the number of days in each month to the array, using 28 for February. Code the program so that it displays the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site