Home >>c programs >C Program to check valid date
In this example, we will see a C program through which we can check if the given date value is a valid date or not.
Algorithm:
/*C program to validate date (Check date is valid or not).*/
#include <stdio.h>
int main()
{
int dd,mm,yy;
printf("Enter date (DD/MM/YYYY format): ");
scanf("%d/%d/%d",&dd,&mm,&yy);
//check year
if(yy>=1900 && yy<=9999)
{
//check month
if(mm>=1 && mm<=12)
{
//check days
if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12))
printf("Date is valid.\n");
else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11))
printf("Date is valid.\n");
else if((dd>=1 && dd<=28) && (mm==2))
printf("Date is valid.\n");
else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0)))
printf("Date is valid.\n");
else
printf("Day is invalid.\n");
}
else
{
printf("Month is not valid.\n");
}
}
else
{
printf("Year is not valid.\n");
}
return 0;
}