...
/Solution Review: Calculate Hours, Minutes, and Seconds
Solution Review: Calculate Hours, Minutes, and Seconds
Let's see the detailed solution review of the challenge given in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
#include <iostream>using namespace std;int main() {// Initialize a variable total_secondsint total_seconds = 3870;// Prints value of total_secondscout << "total_seconds = " << total_seconds << endl;// Declares variableint hours, minutes, seconds;// Convert seconds in hours and store the output in hours variablehours = total_seconds/3600;cout << "Time in hours, minutes and seconds = ";// Prints value of hourscout << hours << "h :";// Store the remaining seconds in total_secondstotal_seconds = total_seconds % 3600;// Convert seconds in minutes and store the output in minutes variableminutes = total_seconds/60;// Prints value of minutescout << minutes << "m :";// Store the remaining seconds in seconds variableseconds = total_seconds % 60;cout << seconds << "s";}
Explanation
Line No. 6: Initializes a variable total_seconds
to 3870
. We have to convert seconds into the number of hours, minutes, and seconds ...