Home >>c programs >C Program to check valid date

C Program to check valid date

C Program to Check if the date is valid or not

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:
  • STEP 1: Enter the date.
  • STEP 2:Check year validation, if the year is not a valid print error.
  • STEP 3:If the year is valid, check month validation, if the month is not a valid print error.
  • STEP 4:If the month is valid, then finally check day validation with leap year condition.
  • STEP 5:If the day is valid print date is a correct otherwise print error.
Program:

/*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;    
}

Output:
Enter date (DD/MM/YYYY format): 15/05/1964
Date is valid.
output
Enter date (DD/MM/YYYY format): 31/02/2010
Day is invalid.

No Sidebar ads