Days In Month
This algorithm finds the total days in a month according to the input date.
static bool IsLeapYear(unsigned int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
static char DaysInMonth(unsigned int year, char month) {
char daysInMonth;
if (month == 4 || month == 6 || month == 9 || month == 11)
{
daysInMonth = 30;
}
else if (month == 2)
{
if (IsLeapYear(year))
{
daysInMonth = 29;
}
else
{
daysInMonth = 28;
}
}
else
{
daysInMonth = 31;
}
return daysInMonth;
}
Example
char value = DaysInMonth(2016, 2);
Output
29