No title

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
using namespace std;

int main() {
  int m, d, y;

  cout << "Enter month: "; cin >> m;
  cout << "Enter day: "; cin >> d;
  cout << "Enter year: "; cin >> y;

  // Determine whether or not the year is a leap year
  bool leap = false;
  if (y % 4 == 0) {
    leap = true;
    if (y % 100 == 0) {
      leap = false;
      if (y % 400 == 0) {
        leap = true;
      }
    }
  }

  // Initially assume that the date is possible
  bool possible = true;

  // Now we check the necessary conditions for the date to be possible,
  // and if we find violations we change the variable to be false.

  // Check that (1 <= day <= 31) and (1 <= month <= 1)
  if (d < 1 || d > 31 || m < 1 || m > 12) 
    possible = false;

  // April, June, September, November have 30 days
  if ((m == 4 || m == 6 || m == 9 || m == 11) && (d > 30)) 
    possible = false;
  
  // February
  if (m == 2) {
    if ((d > 29) || ((!leap) && d == 29))
      possible = false;
  }

  if (possible) {
    cout << "Possible" << endl;
  }
  else {
    cout << "Impossible" << endl;
  }
}