### Help on program to find days in month with leap years Hello!

So I have a code here which determines whether a year is a leap year or not:

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include using namespace std; bool isLeapYear(int year); int main() { int year; cout << "Enter a year: "; cin >> year; if (isLeapYear(year) == false) { cout << year << " is not a leap year. "; } else { cout << year << " is a leap year. "; } return 0; } bool isLeapYear(int year) { bool is_leap_year = false; if (year % 4 == 0) { is_leap_year = true; } if (year % 100 == 0) { is_leap_year = false; } if (year % 400 == 0) { is_leap_year = true; } return is_leap_year; } ``````

I want to make an output like this:

 ``1234`` ``````Enter a year: 2016 2016 is a leap year. Enter a month number: 2 There are 29 days in February 2016``````

I need to use a function that returns the number of days in a given month in a given year, and a function that returns the name of a given month.

Could somebody help? Unsure how to go about this.

Thank you!
Last edited on ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include using namespace std; int month; bool isLeapYear(int year) { bool is_leap_year = false; if (year % 4 == 0) { is_leap_year = true; } if (year % 100 == 0) { is_leap_year = false; } if (year % 400 == 0) { is_leap_year = true; } return is_leap_year; } int getNumberOfDays(int month, int year) { if( month == 2) { if((year%400==0) || (year%4==0 && year%100!=0)) return 29; else return 28; } else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||month == 10 || month==12) return 31; else return 30; } int main() { int year; cout << "Enter a year: "; cin >> year; if (isLeapYear(year) == false) { cout << year << " is not a leap year. "; } else { cout << year << " is a leap year. "; } cout << endl << "Enter a month number: "; cin >> month; int days=0; days = getNumberOfDays(month, year); cout << "There are " << days << " days in this month of " << year << "."; return 0; } ``````

Output:

 ``1234`` ``````Enter a year: 2016 2016 is a leap year. Enter a month number: 2 There are 29 days in this month of 2016.``````

Just need to know now how to change "this month of" to "February".
Last edited on ``1234567`` ``````#include std::string getMonthName(int month) { static std::string months = { "January", "February", "March", "and the rest" }; return months[month - 1]; }``````
Last edited on Edit: Solved, thanks for help.
Last edited on
Registered users can post here. Sign in or register to post.