/* Days In Month Leap Year - This program asks the user to enter the month (letting
the user enter an integer in the range of 1 through 12) and a year. The program
displays the number of days in that month.
It also identifies and displays that a year is a leap year and displays it. The
following applies to determine whether a year is a leap year or not:
* 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. (Ex: 2000 is a leap year, 2001
is not)
* If the year is not divisible by 100, then it is a leap year if and only if it
is divisible by 4. (Ex: 2008 is a leap year, 2009 is not) */
#include "Utility.h"
int main()
{
/* Constants for days in month */
const int THIRTY_DAYS = 30;
const int THIRTYONE_DAYS = 31;
const int TWENTYEIGHT_DAYS = 28;
const int TWENTYNINE_DAYS = 29;
/* Hold months and year */
int month,
year;
/* Hold boolean leapYear*/
bool leapYear = 0;
/* Hold month name and date short (1st, 2nd ... 12th) */
string mName = " ";
string dShort = " ";
/* Ask the user for a month and a year */
cout << "Enter month: ";
cin >> month;
cout << "Enter a year (2000): ";
cin >> year;
switch (month)
{
case 1: mName = "January";
break;
case 2: mName = "February";
break;
case 3: mName = "March";
break;
case 4: mName = "April";
break;
case 5: mName = "May";
break;
case 6: mName = "June";
break;
case 7: mName = "July";
break;
case 8: mName = "August";
break;
case 9: mName = "September";
break;
case 10: mName = "October";
break;
case 11: mName = "November";
break;
case 12: mName = "December";
break;
}
/* Determine dShort */
if (month == 4 || month == 5 || month == 6 || month == 7 || month == 8 ||
month == 9 || month == 10 || month == 11 || month == 12)
{
dShort = "th";
}
else if (month == 1)
{
dShort = "st";
}
else if (month == 2)
{
dShort = "nd";
}
else
{
dShort = "rd";
}
/* Determine whether a year is a leap year or not and display
either year or leap year */
if (year % 100 == 0 || year % 400 == 0 || (year % 4) == 0)
{
leapYear = 1;
cout << "The year is: " << year << " which is a leap year.\n";
}
else
{
leapYear = 0;
cout << "The year is: " << year << endl;
}
/* Determine if month has 28, 30 or 31 days */
if (month != 2)
{
(month == 1 || month == 3 || month == 3 || month == 5 ||
month == 7 || month == 8 || month == 10 || month == 12) ?
cout << "The " << month << dShort << ". month is "
<< mName << " which has: " << THIRTYONE_DAYS << " days\n" :
cout << "The " << month << dShort << ". month is "
<< mName << " which has: " << THIRTY_DAYS << " days\n";
}
else
{
(month == 2 && leapYear != 0) ?
cout << "The " << month << dShort << ". month is "
<< mName << " which has " << TWENTYNINE_DAYS << " days\n" :
cout << "The " << month << dShort << ". month is "
<< mName << " which has " << TWENTYEIGHT_DAYS << " days\n";
}
pauseSystem();
return 0;
}
Saturday, November 19, 2016
Programming Challenge 4.10 - Days In Month Leap Year
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment