+ 1
can anyone help me through writing a code (cpp) or algorithm to find the days and months and years between 2 different dates?
better to be familiar with persian calendar(soalr hijri calendar) but Gregorian calendar is fine too https://code.sololearn.com/cBZ63igtAvVF/?ref=app
5 Réponses
+ 2
Show ur attempt first...We will help with that....
+ 1
athena
This is correct sequence :
cin>>y1>>m1>>d1;
cout<<"enter the second date.\n";
cin>>y2>>m2>>d2;
int y=y2-y1;
int m=m2-m1;
int d=d2-d1;
In your program, calculating y=y2-y1 ; here your are not yet assigned values to y1, y2 so it contains there garbage values...
You should calculate y, m, d after taking all inputs..
It executed and evaluated from top to bottom.
+ 1
For that you need to find a separate Algarithms like for ex: if m1 =1 and d1 = 1 and m2 =11 and d2 = 15, do :
days = m1 has 31 days and d1=1 so add days=31-1=30.
m1++, m1=2 add 28 days 30+28=58
m1++, m1= 3 add 31 days 58+31=89
m1++, m1= 4 add 30 days 89+30=119
m1++, m1= 5 add 31 days 119+31=150
m1++, m1= 6 add 30 days 150+30=180
m1++, m1= 7 add 31 days 180+31=211
m1++, m1= 8 add 31 days 211+31=242
m1++, m1= 9 add 30 days 242+30=272
m1++, m1= 10 add 30 days 272+31=303
m1++, m1= 11 add m1<m2 false, m1 == m2 so add m2 days just 303+15 = 318
Not m1 reamaings days m2 left days are added.
If m1<7 and m1%2==1 add 31 days else 30 days except m1==2 add 28 days
if m1>=7 and m1%2==0 add 31 days else add 30days.
Another way take array as {1, -2,1,0,1,0,1,1,0,1,0,1}
add this days in sequence starts from m1 month, after (m2-m1) *30days. And 1st month days (31-d1)and m2 days..
Hope you can find a simple your way from this i think. Hope it helps..
0
Jayakrishna🇮🇳 but the problem is about the months which has 31 days without them the code works as i know