171k views
5 votes
Suppose John and Bil lworked for sometime and wewant tocalculate the total time both of them worked.Write a program that reads number of days, hours, minutes each of them worked,and prints the total time both of them worked together as days ,hours, minutes.

1 Answer

5 votes

Answer:

We have the following C++ code with appropriate comments

Step-by-step explanation:

#include <iostream>

using namespace std;

//execution of program starts from here

int main()

{

//all instance variables for john

int day_john, hour_john, minutes_john;

//all instance variables for bill

int day_bill, hour_bill, minutes_bill;

//total worked

int total_day, total_hour, total_min;

//after we convert min to hour, we have an extra_hour, which we add in total hour

int extra_hour = 0;

int extra_day = 0;

//ask the total work time of john

cout << "Enter the number of days John worked : ";

cin >> day_john;

cout << "Enter the number of hours John worked : ";

cin >> hour_john;

cout << "Enter the number of minutes John worked : ";

cin >> minutes_john;

//ask the total work time of Bill

cout << "Enter the number of days Bill worked : ";

cin >> day_bill;

cout << "Enter the number of hours Bill worked : ";

cin >> hour_bill;

cout << "Enter the number of minutes Bill worked : ";

cin >> minutes_bill;

//calculate total worked of both together

//if total min are greater than 60, increase the hour by 1

if(minutes_john + minutes_bill > 60)

{

total_min = (minutes_bill + minutes_john)-60;

extra_hour = 1;

}

else

{

total_min = minutes_bill + minutes_john;

}

//similarly for hours

if ((hour_john + hour_bill + extra_hour) > 24)

{

total_hour = (hour_john + hour_bill + extra_hour) - 24;

extra_day =1;

}

else

{

total_hour = hour_john + hour_bill + extra_hour;

}

total_day =day_john + day_bill + extra_day;

//print all

cout << "The total time both of them worked together is: "

<< total_day << " days, " << total_hour << " hours and " << total_min << " min";

return 0;

}

User Justin Kramer
by
5.6k points