OverIQ.com

C Program to check whether a date is valid or not

Last updated on July 27, 2020


The following is a C program to check whether the entered date is valid or not.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/************************************************
 Program to check whether a date is valid or not 
 * 
 * Enter a date (MM/DD/YYYY) :05/30/2002
 * Date is valid.
 * 
 * Enter a date (MM/DD/YYYY) :02/29/2001
 * Date is invalid.
 *************************************************/

#include<stdio.h> // include stdio.h library

int main(void) 
{
    int day, mon, year, is_leap = 0, is_valid = 1;

    printf("Enter a date (MM/DD/YYYY) :");
    scanf("%d/%d/%d", &mon, &day, &year);

    if (year >= 1800 && year <= 9999) 
    {

        //  check whether year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
        {
            is_leap = 1;
        }

        // check whether mon is between 1 and 12
        if(mon >= 1 && mon <= 12)
        {
            // check for days in feb
            if (mon == 2)
            {
                if (is_leap && day == 29) 
                {
                    is_valid = 1;
                }
                else if (day > 28) 
                {
                    is_valid = 0;
                }
            }

            // check for days in April, June, September and November
            else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) 
            {
                if (day > 30)
                {
                    is_valid = 0;
                }
            }

            // check for days in rest of the months 
            // i.e Jan, Mar, May, July, Aug, Oct, Dec
            else if(day > 31)
            {            
                is_valid = 0;
            }        
        }

        else
        {
            is_valid = 0;
        }

    }
    else
    {
        is_valid = 0;
    }

    if(is_valid)
    {
        printf("Date is valid.");
    }

    else
    {
        printf("Date is invalid.");
    }

    return 0; // return 0 to operating system
}

Expected Output: 1st run:

1
2
Enter a date (MM/DD/YYYY) :05/25/2012
Date is valid.

2nd run:

1
2
Enter a date (MM/DD/YYYY) :02/29/2001
Date is invalid.

How it works #

Here are the steps to check whether a date is valid or not.

  1. In line 20, we check whether the entered year is between 1800 and 9999. If the condition is false, the control jumps to the else clause in line 68 and we set the value of is_valid to 0.
  2. Otherwise, if the condition (year >= 1800 && year <= 9999) is true, the control jumps to body of the if statement.
  3. In line 24, we test whether the year is a leap year or not. If the year is a leap year, we set is_leap to 1.
  4. In line 30, we test whether the month is between 1 and 12. If the condition is false, the control jumps to the else clause defined in line 62 and we set the value of is_valid to 0.
  5. Otherwise, if the condition mon >= 1 && mon <= 12 is true, the control comes to the nested if-else statement defined in lines 33-59.
  6. In line 33, we check for invalid days in the month of February.
  7. In line 46, we check for invalid days in the month of April, June, September and November.
  8. In line 56, we check for invalid days in Jan, Mar, May, July, Aug, Oct and Dec.
  9. Finally, in line 73, we check the value of is_valid variable to determine whether the date is valid or not.

Recommended Reading: