C Program to check whether a date is valid or not

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

Expected Output:

1st run:

2nd run:

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:

Leave a Comment

%d bloggers like this: