Printing the date from starting date to ending date
The program takes the starting date and ending date as inputs and prints the dates in between them.This program is useful if u r dealing with dates.
The code
I have used four date functions i.e date1() , date2() , date3() , date4(). When the starting year and ending year is same it performs date1() function.If the month is equal to starting month then it performs date2() function.If the month is equal to ending month it performs date3() function.If the year is in between starting and ending year then it performs date4() function.
Code: Cpp
#include<iostream>
using namespace std;
int date1(int , int , int , int , int , int);
int date2(int , int , int , int );
int date3(int , int , int , int );
int date4(int , int );
main()
{
cout<<"Enter the starting date, month and year"<<endl;
int sd,sm,sy,ed,em,ey;
cin>>sd>>sm>>sy;
cout<<"Enter the ending date, month and year"<<endl;
cin>>ed>>em>>ey;
int i,j,k=0,n,m;
n = ey - sy;
m=sm-em;
if(sy>ey)
{
cout<<"Starting year cannot be greater than ending year"<<endl;
exit(1);
}
if(n==0&&sm>em)
{
cout<<"Date entered is wrong"<<endl;
exit(1);
}
if(n==0&&sm>em&&sd>ed)
{
cout<<"Date entered is wrong"<<endl;
exit(1);
}
if(n==0&&sm==em&&sd>ed)
{
cout<<"Date entered is wrong"<<endl;
exit(1);
}
if(n==0&&m==0)
{
cout<<"Month "<<sm<<" Year "<<sy<<endl;
for(j=sd;j<=ed;j++)
cout<<j<<" "<<endl;
exit(1);
}
if(n==0&&m==0&&sd==ed)
{
cout<<"Month "<<sm<<" Year "<<sy<<endl;
cout<<j<<" "<<endl;
exit(1);
}
if(n==0&&m!=0)
{
for(j=sm;j<=em;j++)
{
date1(j,sy,sm,em,sd,ed);
}
}
else
{//2
for(i=0;i<=n;i++)
{//3
if(i==0)
{
for(j=sm;j<=12;j++)
date2(j,sy,sm,sd);
}
if(i==n)
{
for(j=1;j<=em;j++)
date3(j,ey,em,ed);
}
if(i!=0&&i!=n)
{
for(j=1;j<=12;j++)
date4(j,k);
}
k=sy+1;
}//3
}//2
}//1
int date1(int a , int sy , int sm , int em , int sd , int ed )
{//
//cout<<"1"<<endl;
int i;
if(a==2&&sy%4==0&&(a==sm||a==em))
{//1
cout<<"Month "<<a<<" Year "<<sy<<endl;
if(a==sm)
{
for(i=sd;i<=29;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==em)
{
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
}//1
if(a==2&&sy%4==0&&a!=sm&&a!=em)
{
cout<<"Month "<<a<<" Year "<<sy<<endl;
for(i=1;i<=29;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&sy%4!=0&&(a==sm||a==em))
{//2
cout<<"Month "<<a<<" Year "<<sy<<endl;
if(a==sm)
{
for(i=sd;i<=28;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==em)
{
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
}//2
if(a==2&&sy%4!=0&&a!=sm&&a!=em)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=28;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2==0&&(a==sm||a==em))
{//3
cout<<"Month "<<a<<"Year "<<sy<<endl;
if(a==sm)
{
for(i=sd;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==em)
{
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
}//3
if(a<8&&a!=2&&a%2!=0&&(a==sm||a==em))
{//4
cout<<"Month "<<a<<"Year "<<sy<<endl;
if(a==sm)
{
for(i=sd;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==em)
{
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
}//4
if(a<8&&a!=2&&a%2==0&&a!=sm&&a!=em)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2!=0&&a!=sm&&a!=em)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2==0&&(a==sm||a==em))
{//5
cout<<"Month "<<a<<"Year "<<sy<<endl;
if(a==sm)
{
for(i=sd;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==em)
{
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
}//5
if(a>=8&&a!=2&&a%2!=0&&(a==sm||a==em))
{//6
cout<<"Month "<<a<<"Year "<<sy<<endl;
if(a==sm)
{
for(i=sd;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==em)
{
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
}//6
if(a>=8&&a!=2&&a%2==0&&a!=sm&&a!=em)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2!=0&&a!=sm&&a!=em)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
}//
int date2(int a , int sy , int sm , int sd )
{//
//cout<<"2"<<endl;
int i;
if(a==2&&sy%4==0&&a==sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=sd;i<=29;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&sy%4==0&&a!=sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=29;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&sy%4!=0&&a==sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=sd;i<=28;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&sy%4!=0&&a!=sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=28;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2==0&&a==sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=sd;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2==0&&a!=sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2!=0&&a==sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=sd;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2!=0&&a!=sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2==0&&a==sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=sd;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2==0&&a!=sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2!=0&&a==sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=sd;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2!=0&&a!=sm)
{
cout<<"Month "<<a<<"Year "<<sy<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
}//
int date3(int a , int ey , int em , int ed )
{//
//cout<<"3"<<endl;
int i;
if(a==2&&ey%4==0&&a==em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&ey%4==0&&a!=em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=29;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&ey%4!=0&&a==em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&ey%4!=0&&a!=em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=28;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2==0&&a==em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2==0&&a!=em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2!=0&&a==em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2!=0&&a!=em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2==0&&a==em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2==0&&a!=em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2!=0&&a==em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=ed;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2!=0&&a!=em)
{
cout<<"Month "<<a<<"Year "<<ey<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
}
int date4(int a , int b )
{//
//cout<<"4"<<endl;
int i;
if(a==2&&b%4==0)
{
cout<<"Month "<<a<<"Year "<<b<<endl;
for(i=1;i<=29;i++)
cout<<i<<" ";
cout<<endl;
}
if(a==2&&b%4!=0)
{
cout<<"Month "<<a<<"Year "<<b<<endl;
for(i=1;i<=28;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2==0)
{
cout<<"Month "<<a<<"Year "<<b<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
if(a<8&&a!=2&&a%2!=0)
{
cout<<"Month "<<a<<"Year "<<b<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2==0)
{
cout<<"Month "<<a<<"Year "<<b<<endl;
for(i=1;i<=31;i++)
cout<<i<<" ";
cout<<endl;
}
if(a>=8&&a!=2&&a%2!=0)
{
cout<<"Month "<<a<<"Year "<<b<<endl;
for(i=1;i<=30;i++)
cout<<i<<" ";
cout<<endl;
}
}//


