by Dinesh Thakur Category: Control Structures

Let us use variables dd,mm and yy (all of type int)to represent the day, month and year in a given date. The given date is valid only if year (yy)is non-zero, month (mm)is in the range 1 to 12 and day of month (dd) is in the range 1 to mdays, the number of days in a given month mm. An algorithm to determine the validity of a given date is given below.

valid = 0;

       if (yy != 0)      /* check year */

         {

               if (mm >= 1 && mm <= 12)  /* check month */

                  {  

                       /* determine number of days in given month */

                       int mdays;

                       if (mm == 2)

                           mdays = (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) ? 29 : 28;

                      else if (mm == 4 || mm == 6 || mm == 9 || mm == 11)

                                mdays = 30;

                      else mdays = 31;

                      if (dd >= 1 && dd <= mdays)

                         valid = 1;

                  }

The algorithm is very straight forward. Initially, a flag valid is set to zero (false) indicating that the given date is invalid. Then a nested !(statement sets this flag to 1 (true) if all the required conditions are satisfied. Note that we have to determine mdays, the number of days in a given month, only if the value of mm is valid.

The program given below first accepts a date from the keyboard, determines whether it is valid or not using the algorithm given above and then prints an appropriate message.

Observe that the %d format Specifiers in the scanf format string are separated by '/' characters as "%d/%d/%d". Such non-format characters in the format string of the scanf statement must be entered by the user at the indicated position. Thus, the user must enter the date in the format dd/mm/yyyy.

/* Date validity program */

#include <stdio.h>

void main()

{    

       int dd, mm, yy;    /* given date */

       int valid;             /* flag to indicate date validity */

       clrscr();

       printf("Enter date as dd/mm/yyyy: ");

       scanf("%d/%d/%d", &dd, &mm, &yy);

              /* determine validity of given date */

       valid = 0;

       if (yy != 0)      /* check year */

         {

               if (mm >= 1 && mm <= 12)  /* check month */

                  {  

                       /* determine number of days in given month */

                       int mdays;

                       if (mm == 2)

                           mdays = (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) ? 29 : 28;

                      else if (mm == 4 || mm == 6 || mm == 9 || mm == 11)

                                mdays = 30;

                      else mdays = 31;

                      if (dd >= 1 && dd <= mdays)

                         valid = 1;

                  }

          }

                      printf ("Date is %s\n", valid ? "valid" : "invalid");

                            

                               getch();

}

        C Program Date is Valid or Not



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article