Problem 2 Write the following program in c++. Write a function names DaysOfMonth
ID: 3745593 • Letter: P
Question
Problem 2 Write the following program in c++.
Write a function names DaysOfMonth with two parameters, which are the month and year, to display number of days.(Finding the number of days in a month)
Write a main function to prompt the user to enter the year and month in this order, and call function DaysOfMonth to display 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.
(Hint: the number of days in February for different year may be different, which depends on whether it is a leap year or plain year. A year is leap year if either (1) or (2) holds: (1) it is divisible by 400. (2)it is divisible by 4 but not divisible by 100)
Explanation / Answer
#include<iostream>
using namespace std;
//function declaration
int DaysOfMonth(int month, int year);
int main()
{
int month, year;
cout << "Enter the month followed by year as number: ";
cin >> month>>year;
cout << "Number of days in month " << month << " and year " << year << " is : " << DaysOfMonth(month, year) << endl;
}
//function definition
int DaysOfMonth(int month, int year)
{
//if month is Janaury or Fenruary or March or May or July or August or October or December,there are 31 days in month.
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
return 31;
}
else
{
//if month is February , then check if year is leap year
if (month == 2)
{
if (year % 4 == 0)
{
return 29;
}
else
return 28;
}
//other than February , all other months have 30 days
else
return 30;
}
}
/*output1
Enter the month followed by year as number: 2 2000
Number of days in month 2 and year 2000 is : 29
output2
Enter the month followed by year as number: 3 2018
Number of days in month 3 and year 2018 is : 31
output3
Enter the month followed by year as number: 9 2012
Number of days in month 9 and year 2012 is : 30
//output4
Enter the month followed by year as number: 2 2016
Number of days in month 2 and year 2016 is : 29
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.