Question

Has to be written in C++ Write a program that asks the user to enter the...

Has to be written in C++

Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years:

  1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and only if it is divisible by 400. For example, 2000 is a leap year but 2100 is not.

  2. If the year is not divisible by 100, then it is a leap year if and only if it is divisible by 4. For example, 2008 is a leap year but 2009 is not.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program :-

#include <iostream>
using namespace std;

int main()
{
   int days=0;
int month;
int year;
int valid = 0;
  
while(valid == 0){
cout<< "Please enter the year : "<<endl;
cin>>year;
  
if(year > 0 && year < 2019)
valid = 1;
else
cout<<"Please enter valid year (0-2019)"<<endl;
}
  
valid = 0;
  
  
while(valid == 0){
cout<< "Please enter the month : "<<endl;
cin>>month;
  
if(month >= 1 && month <= 12)
valid = 1;
else
cout<<"Please enter valid month [1-12]"<<endl;
}
  
  
   if( month == 2)
   {
       if((year%400==0) || (year%4==0 && year%100!=0))  
           days = 29;
       else  
           days = 28;
   }
   //months which has 31 days
   else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
   ||month == 10 || month==12)  
       days = 31;
   else       
       days = 30;

   cout<<endl<<"Number of Days in month ="<<month<<", year = "<<year<<" : "<<days;

   return 0;
}

Output :-

Add a comment
Know the answer?
Add Answer to:
Has to be written in C++ Write a program that asks the user to enter the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ code: Days in current month: Write a program that can determine the number of days...

    C++ code: Days in current month: Write a program that can determine the number of days in a month for a specified month and year. The program should allow a user to enter two integers responding a month and a year, and it should determine how many days are in the specified month. The integers 1 through 12 will be used to identify the months of January through December. The user indicates the end of days in the current month...

  • Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

    Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for the month (1 = January, 2 February, etc). l An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and if...

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • Write a program that asks the user to enter a number ofseconds.• There are...

    Starting Out with C++ (9th Edition)  Chapter 4, Problem 7PCWrite a program that asks the user to enter a number of seconds.• There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.• There are 3600 seconds in an hour. If the number of seconds entered by the user is less than 86400, but is greater...

  •    Lab/HW 3   Write a program that reads in the following data, all entered on one...

       Lab/HW 3   Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it’s not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print...

  • Write a C# program that prompts a user to enter a birth month and day. Display...

    Write a C# program that prompts a user to enter a birth month and day. Display an error message if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message.

  • Write a program using M.I.P.S that asks user “how many positive number that is devisable by...

    Write a program using M.I.P.S that asks user “how many positive number that is devisable by 6 you want to add?” .Then your loopcounter would be the user input. If the user enters a positive number between 1 and 100 that is devisable by 6, you increment your loop counter and add it to the sum.. You need to decide if the positive number entered by the user is divisible by 6 or not. Your program should print an error...

  • Write a program named CheckMonth2 that prompts a user to enter a birth month and day....

    Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT