Answer to Question #316474 in C++ for Ankki

Question #316474

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,


1
Expert's answer
2022-03-23T13:45:01-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog