/************************************************
Program to check whether a date is valid or not
*
* Enter a date (MM/DD/YYYY) :05/30/2002
* Date is valid.
*
* Enter a date (MM/DD/YYYY) :02/29/2001
* Date is invalid.
*************************************************/
#include<stdio.h> // include stdio.h library
int main(void)
{
int day, mon, year, is_leap = 0, is_valid = 1;
printf("Enter a date (MM/DD/YYYY) :\n");
scanf("%d/%d/%d", &mon, &day, &year);
if (year >= 1800 && year <= 9999)
{
// check whether year is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
is_leap = 1;
}
// check whether mon is between 1 and 12
if(mon >= 1 && mon <= 12)
{
// check for days in feb
if (mon == 2)
{
if (is_leap && day == 29)
{
is_valid = 1;
}
else if (day > 28)
{
is_valid = 0;
}
}
// check for days in April, June, September and November
else if (mon == 4 || mon == 6 || mon == 9 || mon == 11)
{
if (day > 30)
{
is_valid = 0;
}
}
// check for days in rest of the months
// i.e Jan, Mar, May, July, Aug, Oct, Dec
else if(day > 31)
{
is_valid = 0;
}
}
else
{
is_valid = 0;
}
}
else
{
is_valid = 0;
}
if(is_valid)
{
printf("Date is valid.");
}
else
{
printf("Date is invalid.");
}
return 0; // return 0 to operating system
}