Chapter 4 – #7: Time Calculator – Tony Gaddis – Starting Out With C++

>_ Problem

Write a program that asks the user to enter a number of seconds.

• There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.

• There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3,600, the program should display the number of hours in that many seconds.

• There are 86,400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86,400, the program should display the number of days in that many seconds.

>_ Solution

#include <iostream>
#include <iomanip>  // setprecision() and fixed
using namespace std;

int main()
{
    // Variables
    int seconds;
    float num_mins;

    // Ask the user to enter a number of seconds
    cout << "\nEnter a number of seconds: ";
    cin >> seconds;

    // Format decimal percision at 2 decimal places.
    cout << setprecision(2) << fixed;

    // calculate
    if (seconds <= 59)
    {
        cout << seconds;
        cout << " is less that 60 seconds." << endl;
    }
    else if (seconds >= 60 && seconds < 3600)
    {
        num_mins = seconds / 60.0;
        cout << num_mins << " minutes in ";
        cout << seconds << " seconds" << endl;
    }
    else if (seconds >= 3600 && seconds < 86400)
    {
        num_mins = seconds / 3600.0;
        cout << num_mins << " hours in ";
        cout << seconds << " seconds" << endl;
    }
    else if (seconds >= 86400)
    {
        num_mins = seconds / 86400.0;
        cout << num_mins << " days in ";
        cout << seconds << " seconds" << endl;
    }

    // Formatting
    cout << endl << endl;

    // Terminate program
    return 0;
}

>_Get The Book

Tony Gaddis’ book “Starting Out with C++ from Control Structures to Objects”: 8th and 9th editions.

>_ Video Walk Through

This video includes a step by step process of how I solve programming challenge #7.

>_That’s it!

Thank you for taking interest in what I do! I hope it was helpful for you as much as it helped me along my journey in learning to code!

>_ Also Check Out:

Tag: Starting Out With C++ | Chapter 4 | Programming Challenge Solutions

Leave a Reply