+ 2

please help!!!!.write a program which accepts days as integer and display total no of years ,months and days.

eg if we input 856 days the output should be 2 years 4 months 6 days.

13th Aug 2016, 3:44 PM
Kabeer
Kabeer - avatar
3 odpowiedzi
+ 2
#include <iostream> using namespace std; int main() { int input,years,months,days; cin >> input; years = input / 365; months = (input % 365) / 30; days = (input % 365) - (months * 30); cout << years << " years, " << months << " months, " << days << " days"; return 0; } 856/365 is not exactly 2. But data type of years variable is int. So it will ignore the part after dot.
14th Aug 2016, 9:58 AM
Bekir Uzun
Bekir Uzun - avatar
+ 2
My attempt :) #include <iostream> using namespace std; int main() { int days, months, years; cout << "Enter number of days to convert to years, months and days: "; cin >> days; cout << "\n" << days << " days is "; if (days >= 365) { years = days / 365; days -= years * 365; } else years = 0; if (days >= 30) { months = days / 30; days -= months * 30; } else months = 0; cout << years << " years, " << months << " months and " << days << " days\n"; } Only thing bad about this is that it assumes a month is 30 days, which isn't always the case.
14th Aug 2016, 12:19 PM
Cohen Creber
Cohen Creber - avatar
0
#include<iostream> using namespace std; int main() { int days,y,m,d; cout<<"Enter no. of days : "; cin>>days; y=days/365; days=days%365; m=days/30; d=days%30; cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d; return 0; }
18th Dec 2020, 4:25 AM
Hamdan Khan Official
Hamdan Khan Official - avatar