15. Program that inputs year and month. It displays the number of days in the month of the year entered by the user.
Chapter 5:
Conditional Structures
Programming Exercise
Problem # 15:
Write a program that inputs year and month. It displays the number of days in the month of the year entered by the user. For example, if the user enters 2010 in year and 3 in month, the program should display "March 2010 has 31 days".
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int y,m,month,days;
cout<<"Enter a year and month";
cin>>y>>m;
if(m==1)
cout<<"January "<<y<<" has 31 days.";
else if(m==2)
{
if(y%4!=0)
cout<<"February "<<y<<" has 28 days.";
else
cout<<"February "<<y<<" has 29 days.";
}
else if(m==3)
cout<<"March "<<y<<" has 31 days.";
else if(m==4)
cout<<"April "<<y<<" has 30 days.";
else if(m==5)
cout<<"May "<<y<<" has 31 days.";
else if(m==6)
cout<<"June "<<y<<" has 30 days.";
else if(m==7)
cout<<"July "<<y<<" has 31 days.";
else if(m==8)
cout<<"August "<<y<<" has 31 days.";
else if(m==9)
cout<<"September "<<y<<" has 30 days.";
else if(m==10)
cout<<"October "<<y<<" has 31 days.";
else if(m==11)
cout<<"November "<<y<<" has 30 days.";
else if(m==12)
cout<<"December "<<y<<" has 31 days.";
else
cout<<"The valid input for month is from 1 to 12.";
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
int y,m,month,days;
cout<<"Enter a year and month";
cin>>y>>m;
if(m==1)
cout<<"January "<<y<<" has 31 days.";
else if(m==2)
{
if(y%4!=0)
cout<<"February "<<y<<" has 28 days.";
else
cout<<"February "<<y<<" has 29 days.";
}
else if(m==3)
cout<<"March "<<y<<" has 31 days.";
else if(m==4)
cout<<"April "<<y<<" has 30 days.";
else if(m==5)
cout<<"May "<<y<<" has 31 days.";
else if(m==6)
cout<<"June "<<y<<" has 30 days.";
else if(m==7)
cout<<"July "<<y<<" has 31 days.";
else if(m==8)
cout<<"August "<<y<<" has 31 days.";
else if(m==9)
cout<<"September "<<y<<" has 30 days.";
else if(m==10)
cout<<"October "<<y<<" has 31 days.";
else if(m==11)
cout<<"November "<<y<<" has 30 days.";
else if(m==12)
cout<<"December "<<y<<" has 31 days.";
else
cout<<"The valid input for month is from 1 to 12.";
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
14. Program that inputs the salary of an employee from the user. It deducts the income tax from the salary. The program finally displays salary, income tax and the net salary.
Comments
Post a Comment