C Program Date is Valid or Not: This program will read the date from the user and validate whether the entered date is correct or not with the checking of a leap year.
Enter date.
Check year validation, if the year is not a valid print error.
If the year is valid, check month validation (i.e. month is between 1 to 12), if the month is not a valid print error.
C Program 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);
if(yy>=1900 && yy<=9999)
{
if(mm>=1 && mm<=12)
{
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;
}
Output of Program
Enter date (DD/MM/YYYY format): 10/04/1994
Date is valid.