C Program to check whether the date is valid or invalid - IProgramX

Q. Accept the three positive integers for date from the user (day, month and year) and check whether the date is valid or invalid.


Program

#include<stdio.h>
void main()
{
   int d,m,y;
   int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   int legit = 0;

   printf("Enter the date - DD/MM/YYYY ::  ");
   scanf("%i/%i/%i",&d,&m,&y);
   if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
    daysinmonth[1]=29;
   if (m<13)
   {
      if( d <= daysinmonth[m-1] )
        legit=1;
   }

   if (legit==1)
      printf("It is a valid date!\n");
   else
      printf("It's not valid date!");
}

Output:

Enter the date - DD/MM/YYYY ::  21/03/1998
It is a valid date!

Post a Comment

0 Comments