Leap Year (C++)

#include "stdio.h"
#define TRUE 1
#define FALSE 0

int is_leap_year(int year);

/*-------------------------------------------*/
int date_is_valid(int day, int month, int year)
{
int valid = TRUE;
int month_length[13] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if ( is_leap_year(year) )
month_length[2] = 29;
// 29 days in February in a leap year (including year 2000)

if ( month <> 12 )
valid = FALSE;
else if ( day <> month_length[month] )
valid = FALSE;

return ( valid );
}

/*----------------------*/
int is_leap_year(int year)
{
int result;

if ( (year%4) != 0 ) // or: if ( year%4 )
result = FALSE; // means: if year is not divisible by 4
else if ( (year%400) == 0 ) // or: if ( !(year%400) )
result = TRUE; // means: if year is divisible by 400
else if ( (year%100) == 0 ) // or: if ( !(year%100) )
result = FALSE; // means: if year is divisible by 100
else // (but not by 400, since that case
result = TRUE; // considered already)

return ( result );
}

int main()
{
int day,month,year;
printf("Enter Day Month Year\n");
scanf("%d%d%d",&day,&amp;amp;amp;amp;amp;month,&year);

if(date_is_valid(day,month,year))
{
printf("%d/%d/%d =%d\n",day,month,year,is_leap_year(year));
}

return 1;
}

0 comments: