Write a C++ program that prompts a user for three integers- the first denoting a month (1 to 12), the second denoting a day (1 to 31) and the third denoting a year. The output is displayed as "month day, year" string where month represents the name of the month.
For example, if inputs are 12, 15 and 2020 respectively, the output is December 15,
#include <iostream>
using namespace std;
bool is_leap_year(int y) {
  return (y%4 == 0) && (y%100 != 0 || y%400 == 0);
}
int main() {
  int d, m, y;
  int d_in_m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31,
             30, 31, 30, 31};
  char *month[13] = {"None", "January", "February", "March", "April",
            "May", "June", "July", "August", "September",
            "October", "November", "December"};
 Â
  cin >> m >> d >> y;
 Â
  if (is_leap_year(y)) {
    d_in_m[2]++;
  }
  if (m < 1 || m > 12 || d < 1 || d > d_in_m[m]) {
    cout << "Incorrect data" << endl;
    return 1;
  }
  cout << month[m] << " " << d << ", " << y << endl;
  return 0;
}
Comments
Leave a comment