Programming Challenge Chapter 4 – Q #7 Time Calculator – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 4 – Q #7 Time Calculator  – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 4 – Q #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>
using namespace std;
int main()
{
    // Declare Variables
    int sec;
    int min = 0, hour = 0, day = 0;
    int remaining_sec = 0, remaining_min = 0, remaining_hour = 0;

    // Take Input From the User
    cout << "Enter Second: ";
    cin >> sec;

    // Calculation
    min = sec / 60;
    hour = sec / 3600;
    day = sec / 86400;

    // Calculation For Remaining
    remaining_sec = sec % 60;
    remaining_min = sec % 3600;
    remaining_hour = sec % 86400;

    // Use if else if Condition For Checking
    if (sec < 60)
        cout << "You Enter " << sec << " Second";
    else if (sec >= 60 && sec < 3600)
        cout << min << " Minutss " << remaining_sec << " Seconds";
    else if (sec >= 3600 && sec < 86400)
        cout << hour << " Hours " << remaining_min / 60
             << " Minuts " << remaining_sec << " Seconds";

    else if (sec >= 86400)
        cout << day << " Day " << remaining_hour / 3600 << " Hours "
             << remaining_min / 60 << " Minuts "
             << remaining_sec << " Seconds";

    // return 0 to the main function
    return 0;
}

This is the solution of this question


OUTPUT OF THIS QUESTION



Explanation of this Solution

Solution Explain With Comments in Solution.


Also, I attach CPP file of this problem. You can download this file

Click Here to Download This File


Post a Comment

0 Comments