{ }codelper
integer divisionremainderformatted outputP0013

Seconds to H:MM:SS

An integer s (seconds) is given.
Print this duration as hours:minutes:seconds.
Minutes and seconds always use two digits, with a leading 0 when needed (5 → 05).
Hours are printed as is, without padding, and are not wrapped into days even when 24 or more.

Input
0 ≤ s ≤ 10,000,000
Output
one line H:MM:SS (e.g.
1:02:05, 0:00:45, 27:46:40)
Example 1 · stdin
3725
stdout
1:02:05
Example 2 · stdin
45
stdout
0:00:45
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Hours = s / 3600, minutes = s % 3600 / 60, seconds = s % 60.
Computing minutes as just s / 60 gives 60 or more once past an hour.
Integer division: in C, C++, Java and C# / on two ints is already the quotient, Python uses //, JS/TS need Math.floor, and Craykon has Mathf.Quot(s, 3600).
The most common trap is forgetting the zero padding and printing 1:2:5.
Two-digit padding: C/Java %02d, Python f"{m:02d}", C# {m:D2}, JS/TS String(m).padStart(2, "0"), C++ std::setfill('0') << std::setw(2) — setfill sticks but setw applies only to the next value, so it must be repeated before the seconds.
Craykon has no padding tool, so write a small function that puts "0" in front if (x < 10).

Pythonmain.py · 5 lines
s = int(input())h = s // 3600m = s % 3600 // 60sec = s % 60print(f"{h}:{m:02d}:{sec:02d}")
JavaScriptmain.js · 6 lines
const s = Number(require("fs").readFileSync(0, "utf8").trim());const h = Math.floor(s / 3600);const m = Math.floor((s % 3600) / 60);const sec = s % 60;const two = (x) => String(x).padStart(2, "0");console.log(`${h}:${two(m)}:${two(sec)}`);
TypeScriptmain.ts · 7 lines
import { readFileSync } from "node:fs";const s: number = Number(readFileSync(0, "utf8").trim());const h: number = Math.floor(s / 3600);const m: number = Math.floor((s % 3600) / 60);const sec: number = s % 60;const two = (x: number): string => String(x).padStart(2, "0");console.log(`${h}:${two(m)}:${two(sec)}`);
JavaMain.java · 12 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int s = sc.nextInt();        int h = s / 3600;        int m = s % 3600 / 60;        int sec = s % 60;        System.out.printf("%d:%02d:%02d%n", h, m, sec);    }}
C#Main.cs · 13 lines
using System; class Program{    static void Main()    {        int s = int.Parse(Console.ReadLine().Trim());        int h = s / 3600;        int m = s % 3600 / 60;        int sec = s % 60;        Console.WriteLine($"{h}:{m:D2}:{sec:D2}");    }}
C++main.cpp · 11 lines
#include <iomanip>#include <iostream> int main() {    int s;    std::cin >> s;    int h = s / 3600;    int m = s % 3600 / 60;    int sec = s % 60;    std::cout << h << ':' << std::setfill('0') << std::setw(2) << m << ':' << std::setw(2) << sec << '\n';}
Cmain.c · 11 lines
#include <stdio.h> int main(void) {    int s;    scanf("%d", &s);    int h = s / 3600;    int m = s % 3600 / 60;    int sec = s % 60;    printf("%d:%02d:%02d\n", h, m, sec);    return 0;}
Craykonmain.cray · 14 lines
두자리(x){    if (x < 10)    {        return "0" + x;    }    return "" + x;} s = Console.ReadLine().ToNumber();h = Mathf.Quot(s, 3600);m = Mathf.Quot(s % 3600, 60);sec = s % 60;Console.WriteLine(h + ":" + 두자리(m) + ":" + 두자리(sec));