C++ Code To Print The Number Of Months And Days Between Two Dates In a Year(Used in labrary Applications )
============================================================================= #include <iostream>
using namespace std;
int main()
{
string Mname;
// month,day,year decraring
int day,month,year,day2,month2,year2;
// others stuff used in the programe:
int sub,Maxday;
cout << "\nEnter Start Date======= \n\n" << endl;
cout<<"\nEnter Day : ";
cin>>day;
cout<<"\nEnter Month : ";
cin>>month;
cout<<"\nEnter Year : ";
cin>>year;
cout << "\nEnter End Date ======= \n\n" << endl;
cout<<"\nEnter Day : ";
cin>>day2;
cout<<"\nEnter Month : ";
cin>>month2;
cout<<"\nEnter Year : "<<year<<endl<<endl;
year2=year;
// findind the max date possible in starting month
if ( // checking the starting month
((day<=31 && (month==4||month==6||month==9||month==11))||
(day<=30 && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)) ||
(day<=29 && (month==2)&&(year%4==0)) ||
(day<=28 && (month==2)&&(year%4!=0)))
&&
//checking the end month
((day2<=31&&(month2==4||month2==6||month2==9||month2==11))||
(day2<=30&&(month2==1 || month2==3 || month2==5 || month2==7 || month2==8 || month2==10 || month2==12))||
((day2<=29)&&(month2==2)&&(year2%4==0)) ||
((day2<=28)&&(month2==2)&&(year2%4==0)))
// checking whether month2>month
&&
// if year 2 = year 1 then month 2 must greater than month 1
(
( (year==year2) && (month2>month) )
))
{
for(int i=month;i<=month2;i++){
// month finding
switch(i){
case 1 : Mname="January";
break;
case 2 : Mname="Febraury";
break;
case 3 : Mname="March";
break;
case 4 : Mname="April";
break;
case 5 : Mname="May";
break;
case 6 : Mname="June";
break;
case 7 : Mname="July";
break;
case 8 : Mname="August";
break;
case 9 : Mname="September";
break;
case 10 : Mname="October";
break;
case 11 : Mname="Novemeber";
break;
case 12 : Mname="December";
break;
}
// maximum days in a month::
if(month==4||month==6||month==9||month==11) {Maxday=30;}
else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {Maxday=31;}
else if((month==2)&&(year%4==0)) {Maxday=29;}
else if((month==2)&&(year%4!=0)) {Maxday=28;}
// for starting month
if(i==month){
sub=Maxday-day;
}
// for middle months
else if(i<month2){
if(i==4||i==6||i==9||i==11) {day=30;}
else if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) {day=31;}
else if((i==2)&&(year%4==0)) {day=29;}
else if((i==2)&&(year%4!=0)) {day=28;}
sub=day;
}
// for last month
else if(i==month2)
{sub=day2;}
cout<<sub<<" days in the month of "<<Mname<<" ,"<<year<<"\n";
}
}
else{
cout<<"\nPlease Recheck Your Dates !!\n\n";
}
}
using namespace std;
int main()
{
string Mname;
// month,day,year decraring
int day,month,year,day2,month2,year2;
// others stuff used in the programe:
int sub,Maxday;
cout << "\nEnter Start Date======= \n\n" << endl;
cout<<"\nEnter Day : ";
cin>>day;
cout<<"\nEnter Month : ";
cin>>month;
cout<<"\nEnter Year : ";
cin>>year;
cout << "\nEnter End Date ======= \n\n" << endl;
cout<<"\nEnter Day : ";
cin>>day2;
cout<<"\nEnter Month : ";
cin>>month2;
cout<<"\nEnter Year : "<<year<<endl<<endl;
year2=year;
// findind the max date possible in starting month
if ( // checking the starting month
((day<=31 && (month==4||month==6||month==9||month==11))||
(day<=30 && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)) ||
(day<=29 && (month==2)&&(year%4==0)) ||
(day<=28 && (month==2)&&(year%4!=0)))
&&
//checking the end month
((day2<=31&&(month2==4||month2==6||month2==9||month2==11))||
(day2<=30&&(month2==1 || month2==3 || month2==5 || month2==7 || month2==8 || month2==10 || month2==12))||
((day2<=29)&&(month2==2)&&(year2%4==0)) ||
((day2<=28)&&(month2==2)&&(year2%4==0)))
// checking whether month2>month
&&
// if year 2 = year 1 then month 2 must greater than month 1
(
( (year==year2) && (month2>month) )
))
{
for(int i=month;i<=month2;i++){
// month finding
switch(i){
case 1 : Mname="January";
break;
case 2 : Mname="Febraury";
break;
case 3 : Mname="March";
break;
case 4 : Mname="April";
break;
case 5 : Mname="May";
break;
case 6 : Mname="June";
break;
case 7 : Mname="July";
break;
case 8 : Mname="August";
break;
case 9 : Mname="September";
break;
case 10 : Mname="October";
break;
case 11 : Mname="Novemeber";
break;
case 12 : Mname="December";
break;
}
// maximum days in a month::
if(month==4||month==6||month==9||month==11) {Maxday=30;}
else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {Maxday=31;}
else if((month==2)&&(year%4==0)) {Maxday=29;}
else if((month==2)&&(year%4!=0)) {Maxday=28;}
// for starting month
if(i==month){
sub=Maxday-day;
}
// for middle months
else if(i<month2){
if(i==4||i==6||i==9||i==11) {day=30;}
else if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) {day=31;}
else if((i==2)&&(year%4==0)) {day=29;}
else if((i==2)&&(year%4!=0)) {day=28;}
sub=day;
}
// for last month
else if(i==month2)
{sub=day2;}
cout<<sub<<" days in the month of "<<Mname<<" ,"<<year<<"\n";
}
}
else{
cout<<"\nPlease Recheck Your Dates !!\n\n";
}
}
NOTE
==============
- THIS PROGRAME CAN FIND ONLY NUMBER OF DAYS AND MONTHS INBETWEEN THE DATES IN THE SAME YEAR.
- END MONTH SHOULD BE GREATER THAN START MONTH
- I WILL BE PUBLISHING THE CODE FOR FINDING THE NUMBER OF DAYS AND MONTH IN DIFFERENT YEARS SOON!
- FOR MORE HELP , PLEASE PUT A COMMENT BELOW.
No comments:
Post a Comment