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 = 8The number of days can be isolated if you divide the given integer by 86400 to get the integer quotient

 

$days = intval(700005 / 86400);

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

 

$r = 700005 % 86400;

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

 

$hours = intval(8805 / 3600);

Remaining seconds = 1605The remaining seconds are now

 

$r = 8805 % 3600;

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

 

$minutes = intval(1605 / 60);

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

 

$seconds = 1605 % 60;

Java

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

 

days = (int)(700005 / 86400);

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

 

r = 700005 % 86400;

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

 

hours = (int)(8805 / 3600);

Remaining seconds = 1605The remaining seconds are now

 

r = 8805 % 3600;

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

 

minutes = (int)(1605 / 60);

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

 

seconds = 1605 % 60;

C++

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

 

days = (int)(700005 / 86400);

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

 

r = 700005 % 86400;

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

 

hours = (int)(8805 / 3600);

Remaining seconds = 1605The remaining seconds are now

 

r = 8805 % 3600;

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

 

minutes = (int)(1605 / 60);

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

 

seconds = 1605 % 60;

C#

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

 

days = (int)(700005 / 86400);

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

 

r = 700005 % 86400;

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

 

hours = (int)(8805 / 3600);

Remaining seconds = 1605The remaining seconds are now

 

r = 8805 % 3600;

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

 

minutes = (int)(1605 / 60);

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

 

seconds = 1605 % 60;

Visual Basic

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

 

days = 700005 \ 86400

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

 

r = 700005 Mod 86400

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

 

hours = 8805 \ 3600

Remaining seconds = 1605The remaining seconds are now

 

r = 8805 Mod 3600

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

 

minutes = 1605 \ 60

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

 

seconds = 1605 Mod 60

Python

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

 

days = 700005 // 86400

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

 

r = 700005 % 86400

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

 

hours = 8805 // 3600

Remaining seconds = 1605The remaining seconds are now

 

r = 8805 % 3600

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

 

minutes = 1605 // 60

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

 

seconds = 1605 % 60

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")