Answer to Question #125072 in C++ for Zeeshan Ali

Question #125072
Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days.
1
Expert's answer
2020-07-07T09:56:05-0400
#include <stdio.h>
#define BUFSIZE 8192
void input(const char *s, int *d) {
    char buffer[BUFSIZE];
    printf("%s",s);
    fgets(buffer, sizeof(buffer), stdin);
    sscanf(buffer,"%d",d);
}
int main(void) {
    int month=0,year=0;
    const int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    input("Enter month number, 1-12: ",&month);
    input("Enter year number, 1-9999: ",&year);
    printf("%d\n",days[month-1]+(
        (month==2)&&(
            (year%4==0)-(year%100==0)+(year%400==0)
        )
    ));
    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
APPROVED BY CLIENTS