Answer to Question #128826 in C# for kern

Question #128826
There is an int variable that stores a certain amount of time in seconds. I need a function that converts seconds to days, hours, minutes and seconds and returns this as a string.
1
Expert's answer
2020-08-11T10:41:36-0400
#include <iostream>
#include <string>
using namespace std;

string calculate(int seconds) {
    int secondsPerMinute = 60;
    int secondsPerHour = secondsPerMinute * 60;
    int secondsPerDay = secondsPerHour * 24;
    
    int days = seconds / secondsPerDay;
    seconds %= secondsPerDay;
    int hours = seconds / secondsPerHour;
    seconds %= secondsPerHour;
    int minutes = seconds / secondsPerMinute;
    seconds %= secondsPerMinute;
    
    
    string result =
            "Days: " + to_string(days) +
            "\nHours: " + to_string(hours) +
            "\nMinutes: " + to_string(minutes) +
            "\nSeconds: " + to_string(seconds);
            
    return result;
}

int main() {
    int seconds;
    cin >> seconds;
    cout << calculate(seconds);
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS