0% found this document useful (0 votes)
44 views2 pages

As3 - 2023-05-23T104042.003

This C++ program asks the user to input a month and year, then uses an if/else statement and switch case to output the number of days in that month based on whether it is a leap year or not. It checks that the year is between 0-9000, then uses cases to handle months 1-12, and defaults to an error message for invalid input.

Uploaded by

Def Abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

As3 - 2023-05-23T104042.003

This C++ program asks the user to input a month and year, then uses an if/else statement and switch case to output the number of days in that month based on whether it is a leap year or not. It checks that the year is between 0-9000, then uses cases to handle months 1-12, and defaults to an error message for invalid input.

Uploaded by

Def Abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

Name: Le Cong Thanh Khoa


Class: 21 ECE
*/

#include <iostream>
using namespace std;

int main()
{
int year,month;

cout << "Days in a Month\n";


cout << "Enter the month (1 - 12): "; cin >> month;
cout << "Enter the year (up to 9000): "; cin >> year;

cout << endl;


if (year >= 0 && year <= 9000)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout << "31 days";
break;

case 4:
case 6:
case 9:
case 11:
cout << "30 days";
break;

case 2:
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << "29 days" << endl;
else
cout << "28 days" << endl;
}
else if (year % 100 != 0)
{
if (year % 4 == 0)
cout << "29 days" << endl;
else
cout << "28 days" << endl;
}
break;

default:
cout << "Invalid month. Rerun program. Try again." << endl;
}
}
else
{
cout << "Invalid year. Rerun program. ";
cout << "Try again." << endl;
}

return 0;
}

You might also like