Thursday, 26 May 2016
Q: 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".
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year, month, numofdaysinmonth=0;
cout<<"Enter a year:";
cin>>month;
cout<<"Enter a month in the year( e.g 1 for Jan): ";
cin>>month;
switch(month)
{
case 1:
cout<<" January "<<year;
numofdaysinmonth=31;
break;
case 2:
cout<<" February "<<year;
if(year%400==0 || year%4==0 || year%100!=0)
numofdaysinmonth=29;
else
numofdaysinmonth=28;
break;
case 3:
cout<< " March "<<year;
numofdaysinmonth=31;
break;
case 4:
cout<<" April "<<year;
numofdaysinmonth =30;
break;
case 5:
cout<<" May "<<year;
numofdaysinmonth= 31;
break;
case 6:
cout<<" June "<<year;
numofdaysinmonth= 30;
break;
case 7:
cout<<" July "<<year;
numofdaysinmonth = 31;
break;
case 8:
cout<< " August "<<year;
numofdaysinmonth = 31;
break;
case 9:
cout<<" September "<<year;
numofdaysinmonth = 30;
break;
case 10:
cout<<" October "<<year;
numofdaysinmonth = 31;
break;
case 11:
cout<<" November " <<year;
numofdaysinmonth = 30;
break;
case 12:
cout<<" December "<<year;
numofdaysinmonth =31;
break;
}
cout<<" has "<<numberofdaysinmonth<< " days ";
getche();
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment