Date Comparator

Date Comparator

Easy C Structures 27 views
Explanation Complexity

Problem Statement

Create a date structure (day, month, year). Input two dates and decide which one is first. Handle edge cases: leap year, invalid dates, odd dates

Input Format

Two dates entered by the user.
Each date has:

• day

• month

• year

Output Format

• "Date 1 is earlier"

• "Date 2 is earlier"

• "Both dates are same"
OR

• "Invalid date"

Example

Date 1: 15 08 2020  
Date 2: 10 01 2021
Date 1 is earlier

Constraints

• Year > 0

• Month between 1 and 12

• Day must be valid for the given month and year

Concept Explanation

Dates are compared in order: year → month → day.
Before comparison, each date must be checked for validity.

Step-by-Step Explanation

1.Check month is between 1 and 12.

2.Check leap year (for February).

3.Check day is valid for that month.

4.If any date is invalid → print Invalid date.

5.Compare years.

6.If years same, compare months.

7.If months same, compare days.

8.Decide earlier date or same date.

Concept Explanation

Dates are compared in order: year → month → day.
Before comparison, each date must be checked for validity.

Step-by-Step Explanation

1.Check month is between 1 and 12.

2.Check leap year (for February).

3.Check day is valid for that month.

4.If any date is invalid → print Invalid date.

5.Compare years.

6.If years same, compare months.

7.If months same, compare days.

8.Decide earlier date or same date.

Input / Output Format

Input Format
Two dates entered by the user.
Each date has:

• day

• month

• year
Output Format
• "Date 1 is earlier"

• "Date 2 is earlier"

• "Both dates are same"
OR

• "Invalid date"
Constraints
• Year > 0

• Month between 1 and 12

• Day must be valid for the given month and year

Examples

Input:
Date 1: 15 08 2020 Date 2: 10 01 2021
Output:
Date 1 is earlier

Example Solution (Public)

C
#include <stdio.h>

// Date structure
struct Date {
    int day;
    int month;
    int year;
};

// Function to check leap year
int isLeapYear(int year) {
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
        return 1;
    return 0;
}

// Function to validate date
int isValidDate(struct Date d) {
    int daysInMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    if (d.year < 1)
        return 0;

    if (d.month < 1 || d.month > 12)
        return 0;

    if (isLeapYear(d.year))
        daysInMonth[2] = 29;

    if (d.day < 1 || d.day > daysInMonth[d.month])
        return 0;

    return 1;
}

// Function to compare two dates
int compareDates(struct Date d1, struct Date d2) {
    if (d1.year != d2.year)
        return d1.year < d2.year;

    if (d1.month != d2.month)
        return d1.month < d2.month;

    return d1.day < d2.day;
}

int main() {
    struct Date d1, d2;

    printf("Enter first date (DD MM YYYY): ");
    scanf("%d %d %d", &d1.day, &d1.month, &d1.year);

    printf("Enter second date (DD MM YYYY): ");
    scanf("%d %d %d", &d2.day, &d2.month, &d2.year);

    // Validate dates
    if (!isValidDate(d1) || !isValidDate(d2)) {
        printf("nInvalid date entered.n");
        return 0;
    }

    // Compare dates
    if (compareDates(d1, d2))
        printf("nFirst date comes before second date.n");
    else if (compareDates(d2, d1))
        printf("nSecond date comes before first date.n");
    else
        printf("nBoth dates are the same.n");

    return 0;
}

Official Solution Code

#include <stdio.h>

// Date structure
struct Date {
    int day;
    int month;
    int year;
};

// Function to check leap year
int isLeapYear(int year) {
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
        return 1;
    return 0;
}

// Function to validate date
int isValidDate(struct Date d) {
    int daysInMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    if (d.year < 1)
        return 0;

    if (d.month < 1 || d.month > 12)
        return 0;

    if (isLeapYear(d.year))
        daysInMonth[2] = 29;

    if (d.day < 1 || d.day > daysInMonth[d.month])
        return 0;

    return 1;
}

// Function to compare two dates
int compareDates(struct Date d1, struct Date d2) {
    if (d1.year != d2.year)
        return d1.year < d2.year;

    if (d1.month != d2.month)
        return d1.month < d2.month;

    return d1.day < d2.day;
}

int main() {
    struct Date d1, d2;

    printf("Enter first date (DD MM YYYY): ");
    scanf("%d %d %d", &d1.day, &d1.month, &d1.year);

    printf("Enter second date (DD MM YYYY): ");
    scanf("%d %d %d", &d2.day, &d2.month, &d2.year);

    // Validate dates
    if (!isValidDate(d1) || !isValidDate(d2)) {
        printf("nInvalid date entered.n");
        return 0;
    }

    // Compare dates
    if (compareDates(d1, d2))
        printf("nFirst date comes before second date.n");
    else if (compareDates(d2, d1))
        printf("nSecond date comes before first date.n");
    else
        printf("nBoth dates are the same.n");

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.