Thursday, 11 February 2016

Dealing with Data (C++ Primer Plus Sixth Edition Solution Manual) Question-4

Leave a Comment
Questions:

Write a program that asks the user to enter the number of seconds as an integer value (use type long, or, if available, long long) and that then displays the equivalent time in days, hours, minutes, and seconds. Use symbolic constants to represent the number of hours in the day, the number of minutes in an hour, and the number of seconds in a minute.The output should look like this:

Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds


Explanation:

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/


#include <iostream>

using namespace std;

void main() {
       const int HourPerDay = 24;
       const int MinPerHour = 60;
       const int SecPerMin = 60;

       cout << "Enter the number of seconds:";
       long input;
       cin >> input;

       long min = input / SecPerMin;
       int sec = input % SecPerMin;

       long hour = min / MinPerHour;
       int min_remain = min % MinPerHour;

       long day = hour / HourPerDay;
       int hour_remain = hour % HourPerDay;

       cout << input
              << " seconds = "
              << day
              << " days, "
              << hour_remain
              << " hours, "
              << min_remain
              << " minutes, "
              << sec
              << " seconds"
              << endl;
}



Output:
C++ Programs And Projects


Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

 

 


If You Enjoyed This, Take 5 Seconds To Share It