C Program to Date Comparator with Explanation
C
Easy
Structures
28 views
2 min read
212 words
This problem helps you practice core C fundamentals in a practical way. It builds intuition around date, year, month. Let’s break it down step by step so you can implement it confidently.
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"
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 Logic
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.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
#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;
}
Output Example
Input:
Date 1: 15 08 2020
Date 2: 10 01 2021
Output:
Date 1 is earlier
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
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 / Output
Input
Two dates entered by the user.
Each date has:
• day
• month
• year
Output
• "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
Explanation
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.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
#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;
}
Solutions (0)
No solutions submitted yet. Be the first!