Write a program that prompts the user to enter an integer that represents an elapsed time in seconds and then displays it in the format “DD days HH hours MM minutes and SS seconds”. For example if the user enters the number 700005, the message “8 days 2 hours 26 minutes and 45 seconds” must be displayed.
Solution
As you may already know, there are 60 seconds in a minute, 3600 seconds in an hour, and 86400 seconds in a day.
Let’s try to analyze number 700005 using the first approach.
PHP
Days = 8  The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

Remaining seconds = 8805  The remaining seconds can be isolated if you divide the given integer by 86400 to get the integer remainder

Hours = 2  The number of hours can be isolated if you divide the remaining seconds by 3600 to get the integer quotient

Remaining seconds = 1605  The remaining seconds are now

Minutes = 26  The number of minutes can be isolated if you divide the remaining seconds by 60 to get the integer quotient

Seconds = 45  The last remainder, which happens to be the number of seconds left, is

Java
Days = 8  The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

Remaining seconds = 8805  The remaining seconds can be isolated if you divide the given integer by 86400 to get the integer remainder

Hours = 2  The number of hours can be isolated if you divide the remaining seconds by 3600 to get the integer quotient

Remaining seconds = 1605  The remaining seconds are now

Minutes = 26  The number of minutes can be isolated if you divide the remaining seconds by 60 to get the integer quotient

Seconds = 45  The last remainder, which happens to be the number of seconds left, is

C++
Days = 8  The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

Remaining seconds = 8805  The remaining seconds can be isolated if you divide the given integer by 86400 to get the integer remainder

Hours = 2  The number of hours can be isolated if you divide the remaining seconds by 3600 to get the integer quotient

Remaining seconds = 1605  The remaining seconds are now

Minutes = 26  The number of minutes can be isolated if you divide the remaining seconds by 60 to get the integer quotient

Seconds = 45  The last remainder, which happens to be the number of seconds left, is

C#
Days = 8  The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

Remaining seconds = 8805  The remaining seconds can be isolated if you divide the given integer by 86400 to get the integer remainder

Hours = 2  The number of hours can be isolated if you divide the remaining seconds by 3600 to get the integer quotient

Remaining seconds = 1605  The remaining seconds are now

Minutes = 26  The number of minutes can be isolated if you divide the remaining seconds by 60 to get the integer quotient

Seconds = 45  The last remainder, which happens to be the number of seconds left, is

Visual Basic
Days = 8  The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

Remaining seconds = 8805  The remaining seconds can be isolated if you divide the given integer by 86400 to get the integer remainder

Hours = 2  The number of hours can be isolated if you divide the remaining seconds by 3600 to get the integer quotient

Remaining seconds = 1605  The remaining seconds are now

Minutes = 26  The number of minutes can be isolated if you divide the remaining seconds by 60 to get the integer quotient

Seconds = 45  The last remainder, which happens to be the number of seconds left, is

Python
Days = 8  The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

Remaining seconds = 8805  The remaining seconds can be isolated if you divide the given integer by 86400 to get the integer remainder

Hours = 2  The number of hours can be isolated if you divide the remaining seconds by 3600 to get the integer quotient

Remaining seconds = 1605  The remaining seconds are now

Minutes = 26  The number of minutes can be isolated if you divide the remaining seconds by 60 to get the integer quotient

Seconds = 45  The last remainder, which happens to be the number of seconds left, is

The program for this algorithm is shown here.
PHP
<?php
echo "Enter a period of time in seconds: ";
$number = trim(fgets(STDIN));
$days = intval($number / 86400); // 60 * 60 * 24 = 86400
$r = $number % 86400;
$hours = intval($r / 3600); // 60 * 60 = 3600
$r = $r % 3600;
$minutes = intval($r / 60);
$seconds = $r % 60;
echo $days, " days ", $hours, " hours ";
echo $minutes, " minutes and ", $seconds, " seconds";
?>
Java
public static void main(String[] args) throws java.io.IOException {
java.io.BufferedReader cin = new java.io.
BufferedReader(new java.io.InputStreamReader(System.in));
int days, hours, minutes, number, r, seconds;
System.out.print("Enter a period of time in seconds: ");
number = Integer.parseInt(cin.readLine());
days = (int)(number / 86400); // 60 * 60 * 24 = 86400
r = number % 86400;
hours = (int)(r / 3600); // 60 * 60 = 3600
r = r % 3600;
minutes = (int)(r / 60);
seconds = r % 60;
System.out.println(days + " days " + hours + " hours ");
System.out.println(minutes + " minutes and " + seconds + " seconds");
}
C++
#include <iostream>
using namespace std;
int main() {
int days, hours, minutes, number, r, seconds;
cout << "Enter a period of time in seconds: ";
cin >> number;
days = (int)(number / 86400); // 60 * 60 * 24 = 86400
r = number % 86400;
hours = (int)(r / 3600); // 60 * 60 = 3600
r = r % 3600;
minutes = (int)(r / 60);
seconds = r % 60;
cout << days << " days " << hours << " hours " << endl;
cout << minutes << " minutes and " << seconds << " seconds";
return 0;
}
C#
static void Main() {
int days, hours, minutes, number, r, seconds;
Console.Write("Enter a period of time in seconds: ");
number = Int32.Parse(Console.ReadLine());
days = (int)(number / 86400); // 60 * 60 * 24 = 86400
r = number % 86400;
hours = (int)(r / 3600); // 60 * 60 = 3600
r = r % 3600;
minutes = (int)(r / 60);
seconds = r % 60;
Console.WriteLine(days + " days " + hours + " hours ");
Console.Write(minutes + " minutes and " + seconds + " seconds");
Console.ReadKey();
}
Visual Basic
Sub Main()
Dim days, hours, minutes, number, r, seconds As Integer
Console.Write("Enter a period of time in seconds: ")
number = Console.ReadLine()
days = number \ 86400 ' 60 * 60 * 24 = 86400
r = number Mod 86400
hours = r \ 3600 ' 60 * 60 = 3600
r = r Mod 3600
minutes = r \ 60
seconds = r Mod 60
Console.WriteLine(days & " days " & hours & " hours ")
Console.Write(minutes & " minutes and " & seconds & " seconds")
Console.ReadKey()
End Sub
Python
number = int(input("Enter a period of time in seconds: "))
days, r = divmod(number, 86400) # 60 * 60 * 24 = 86400
hours, r = divmod(r, 3600) # 60 * 60 = 3600
minutes, seconds = divmod(r, 60)
print(days, "days", hours, "hours ")
print(minutes, "minutes and", seconds, "seconds")
Leave a Reply