The MUSEUM has asked you to write a C++ applicationt that children can use to find the total number of hours and years slept during their lifetime. Assume they slept at an average of 8 hour per night. the user should enter the name, birthday, year and current year. To calculate the number of hours slept. 365 day per year, 30 day per month and 8 hours of sleep per day. The program should show how many years, month, weeks, days, hours, minutes, seconds the child slept in this lifetime
#include <iostream>
using namespace std;
void totalSleep(int day, int month, int year, int curYear) {
int days = ((30*(12-month) + 30-day + (curYear-year-1)*365)*8)/24;
int years = days/365;
int months = days/30;
int weeks = days/7;
int hours = (30*(12-month) + 30-day + (curYear-year-1)*365)*8;
int minutes = hours*60;
int seconds = minutes*60;
cout << "You slept " <<
years << " years, " <<
months << " months, " <<
weeks << " weeks, " <<
days << " days, " <<
hours << " hours, " <<
minutes << " minutes, " <<
seconds << " seconds.";
}
int main() {
int day, month, year, curYear;
cout << "Input a birthday (1-30): ";
cin >> day;
cout << "Input a birth month (1-12): ";
cin >> month;
cout << "Input a birth year: ";
cin >> year;
cout << "Input a current year: ";
cin >> curYear;
totalSleep(day, month, year, curYear);
return 0;
}
Example:
Input a birthday (1-30): 7
Input a birth month (1-12): 3
Input a birth year: 2002
Input a current year: 2021
Output:
You slept 6 years, 76 months, 326 weeks, 2287 days, 54904 hours, 3294240 minutes, 197654400 seconds.
Comments
Leave a comment