+ 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.
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.
+ 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.
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;
}