C Program to print the earlier of the two dates

The following C program asks the user to enter two dates and prints the earlier of the two dates.

Expected Output:

1st run:

2nd run:

How it works

The program starts off by asking the user to enter two dates.

In line 27 and 32, we check whether the dates entered is valid or not using a user-defined check_date() function. To learn more about how check_date() function works visit this link.

If either of the dates is invalid, the program terminates with the appropriate error message.

In lines 39-85, we have an if-else statement which compares the two dates. The algorithm to compare the dates work as follows:

  • If year1 > year2, the second date comes earlier than the first.
  • If year1 < year2, the first date comes earlier than the second.
  • If year1 == year2. The following conditions are possible:
    • If mon1 == mon2, The following conditions are possible:
      • If day1 == day2, both dates are the same.
      • If day1 > day2, then the second date comes earlier than the first.
      • If day1 < day2, then the first date comes earlier than the second.
    • If mon1 > mon2, the second date comes earlier than the first.
    • Otherwise, mon1 < mon2 and the first date comes earlier than the second.

 

 

Leave a Comment

%d bloggers like this: