C program for date validation.

#include<stdio.h>
#include<conio.h>
void main() {
int dd,mm,yyyy,md,flag=1;
clrscr();
printf("Enter dd/mm/yyyy\n");
scanf("%d%d%d",&dd,&mm,&yyyy);
switch (mm) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
md=31; break;
case 4: case 6: case 9: case 11: 
md=30; break;
case 2: 
if (yyyy%4==0) 
md=29;
else md=28; 
break;
default: flag=0;
printf("\nInvalid month%d\n",mm);
}
if (flag==1) {
if (yyyy>=1900 && yyyy<=2100) {
if (dd>=1 && dd<=md)
printf("\nGiven date %d/%d/%d is vaid date\n",dd,mm,yyyy);
else printf("\nInvalid day %d\n",dd);
}
else printf("%d is not in given year range 1900 to 2100\n",yyyy);
}
getch();
}

No comments:

Post a Comment