Write a program to input a given specific date and check whether the date is valid or not. Year will be in range 1900 to 9999.
31 Days: Jan, Mar, May, Jul, Aug, Oct, Dec (7 months) 30 Days: Apr, Jun, Sep, Nov (4 months) 28/29 Days: Feb (depends on leap year) (1 month) Leap Year Detection: โโ Divisible by 400? โ YES (Leap Year) โโ Divisible by 100? โ NO (Not Leap) โโ Divisible by 4? โ YES (Leap Year) โโ Otherwise โ NO (Not Leap) Example: 29/02/1998 โ โ โ 29 02 1998 ? โ โ Is 1998 a leap year? รท 400? No รท 100? No รท 4? No (1998รท4 = 499.5) โ Not a leap year โ February has only 28 days โ 29th February doesn't exist! Result: Invalid โ
Accept three integer as a input separated by a '/'
Print the date is "Valid" or "Invalid"
1 <= INPUT <= 9999
#include <iostream> using namespace std; int main() { int day, month, year; char slash1, slash2; cin >> day >> slash1 >> month >> slash2 >> year; // Check year range if (year < 1900 || year > 9999) { cout << "Invalid" << endl; return 0; } // Check month range if (month < 1 || month > 12) { cout << "Invalid" << endl; return 0; } // Determine maximum days in the month int maxDays; if (month == 2) { // February // Leap year detection bool isLeapYear = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); maxDays = isLeapYear ? 29 : 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { // April, June, September, November maxDays = 30; } else { // January, March, May, July, August, October, December maxDays = 31; } // Check if day is valid for this month if (day >= 1 && day <= maxDays) { cout << "Valid" << endl; } else { cout << "Invalid" << endl; } return 0; }
Gregorian Calendar Rules:
// Leap year detection algorithm: bool isLeapYear = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); // Breaking it down: // Condition 1: Divisible by 400 (year % 400 == 0) // Examples: 1600, 2000, 2400 โ Leap years // OR // Condition 2: Divisible by 4 BUT NOT by 100 (year % 4 == 0 && year % 100 != 0) // Examples: 2004, 2008, 2020 โ Leap years // But NOT: 1700, 1800, 1900 โ Not leap years
Test Cases for Leap Years:
Year | รท400? | รท100? | รท4? | Leap Year? | Explanation |
---|---|---|---|---|---|
2000 | โ | โ | โ | YES | Divisible by 400 |
1900 | โ | โ | โ | NO | Century year not รท400 |
2004 | โ | โ | โ | YES | รท4 but not รท100 |
1998 | โ | โ | โ | NO | Not divisible by 4 |
2024 | โ | โ | โ | YES | รท4 but not รท100 |
Month # | Month Name | Days | Memory Aid |
---|---|---|---|
1 | January | 31 | 31 days: Jan, Mar, May, Jul, Aug, Oct, Dec |
2 | February | 28/29 | Leap year dependent |
3 | March | 31 | |
4 | April | 30 | 30 days: Apr, Jun, Sep, Nov ("Thirty days hath...") |
5 | May | 31 | |
6 | June | 30 | |
7 | July | 31 | |
8 | August | 31 | Jul & Aug both 31! |
9 | September | 30 | |
10 | October | 31 | |
11 | November | 30 | |
12 | December | 31 |
Memory Rhyme:
Thirty days hath September, April, June, and November. All the rest have thirty-one, Except for February alone, Which has twenty-eight days clear, And twenty-nine in each leap year.
Input: day/month/year โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ Step 1: Validate Year โ โ Is 1900 โค year โค 9999? โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ NO โ Invalid โ YES โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ Step 2: Validate Month โ โ Is 1 โค month โค 12? โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ NO โ Invalid โ YES โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ Step 3: Determine โ โ Max Days for Month โ โ โ โ โโ Month == 2? โ โ โโโโ YES โ Leap Year? โ โ โ โโ YES โ 29 โ โ โ โโ NO โ 28 โ โ โโ Month in {4,6,9,11}?โ โ โ โโ YES โ 30 โ โ โโ Else โ 31 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ Step 4: Validate Day โ โ Is 1 โค day โค maxDays? โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ NO โ Invalid โ YES Valid!
Edge Case | Example | Expected | Why |
---|---|---|---|
Leap year Feb 29 | 29/02/2000 | Valid | 2000 รท 400 = leap |
Non-leap Feb 29 | 29/02/1998 | Invalid | 1998 not leap year |
Century non-leap | 29/02/1900 | Invalid | 1900 not รท 400 |
April 31st | 31/04/2023 | Invalid | April has 30 days |
Month 13 | 15/13/2023 | Invalid | Only 12 months |
Day 0 | 0/05/2023 | Invalid | Days start at 1 |
Year 1899 | 15/05/1899 | Invalid | Before 1900 |
Year 10000 | 15/05/10000 | Invalid | After 9999 |
Dec 31st | 31/12/2023 | Valid | Last day of year |
Jan 1st 1900 | 1/01/1900 | Valid | First allowed date |
Date validation is one of the most common validation tasks in programming. Incorrect date handling can lead to:
This problem teaches you to handle one of programming's fundamental challenges correctly!