Write a program to read the total seconds and print the seconds in time format Example :hr:min:sec
The Challenge: Breaking Down Time Units
Converting total seconds into hours, minutes, and seconds is a classic problem that teaches you how to decompose a value into multiple units. This is fundamental to understanding time, currency, and any hierarchical unit system!
The Time Hierarchy:
1 minute = 60 seconds
1 hour = 60 minutes = 3600 seconds
1 day = 24 hours = 86400 seconds
// So for 54300 seconds:
// How many full hours? 54300 รท 3600 = 15 hours
// Remaining seconds? 54300 % 3600 = 300 seconds
// How many full minutes? 300 รท 60 = 5 minutes
// Remaining seconds? 300 % 60 = 0 seconds
long long totalSec = 54300;
// Step 1: Extract HOURS
long long hours = totalSec / 3600;
// 54300 / 3600 = 15 hours
// Step 2: Get remaining seconds after removing hours
totalSec = totalSec % 3600;
// 54300 % 3600 = 300 seconds left
// Step 3: Extract MINUTES from remaining seconds
long long minutes = totalSec / 60;
// 300 / 60 = 5 minutes
// Step 4: Get remaining SECONDS
long long seconds = totalSec % 60;
// 300 % 60 = 0 seconds
// Result: 15:HOUR 05:MIN :00SEC โ
Operation | Symbol | Purpose | Example |
---|---|---|---|
Division | / |
Counts how many times divisor fits | 54300 / 3600 = 15 |
Modulo | % |
Gets the remainder | 54300 % 3600 = 300 |
/
tells you "how many complete units"%
tells you "what's left over"// Input: 76000 seconds
// Step 1: Get hours
hours = 76000 / 3600 = 21
// 21 complete hours
// Step 2: Remove hours, get remainder
remaining = 76000 % 3600 = 400
// 400 seconds left after removing 21 hours
// Step 3: Get minutes from remainder
minutes = 400 / 60 = 6
// 6 complete minutes
// Step 4: Get final seconds
seconds = 400 % 60 = 40
// 40 seconds remain
// Output: 21:HOUR 06:MIN :40SEC โ
// Verification:
// (21 ร 3600) + (6 ร 60) + 40 = 75600 + 360 + 40 = 76000 โ
Formatting the Output with Leading Zeros:
#include <iomanip>
// setfill('0') - Fill empty spaces with '0'
// setw(2) - Set width to 2 characters
cout << setfill('0') << setw(2) << 5; // Prints: "05"
cout << setfill('0') << setw(2) << 15; // Prints: "15"
// Why we need this:
// Without formatting: "15:HOUR 5:MIN :0SEC" (looks wrong!)
// With formatting: "15:HOUR 05:MIN :00SEC" (perfect!)
Manipulator | Purpose | Example |
---|---|---|
setfill('0') |
Set the fill character | Fills empty spaces with '0' |
setw(2) |
Set minimum width | Ensures at least 2 digits |
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long long sec;
cin >> sec;
// Extract hours (3600 seconds = 1 hour)
long long hours = sec / 3600;
sec = sec % 3600; // Update sec to remainder
// Extract minutes (60 seconds = 1 minute)
long long minutes = sec / 60;
sec = sec % 60; // Final seconds
// Format and print with leading zeros
cout << setfill('0') << setw(2) << hours << ":HOUR "
<< setw(2) << minutes << ":MIN :"
<< setw(2) << sec << "SEC";
return 0;
}
This technique is used everywhere:
// Convert cents to dollars and cents
int totalCents = 567;
int dollars = totalCents / 100; // 5 dollars
int cents = totalCents % 100; // 67 cents
// Convert days to years, months, days (simplified)
int totalDays = 400;
int years = totalDays / 365; // 1 year
int days = totalDays % 365; // 35 days
// Convert bytes to GB, MB, KB
long long bytes = 5368709120; // 5 GB
long long gb = bytes / (1024*1024*1024);
bytes = bytes % (1024*1024*1024);
long long mb = bytes / (1024*1024);
/
and %
is a fundamental pattern for decomposing values into hierarchical units. Master this,
and you can convert anything!
a / b
and a % b
:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long long sec;
cin >> sec;
long long hours = sec / 3600;
sec = sec % 3600;
long long minutes = sec / 60;
sec = sec % 60;
cout << setfill('0') << setw(2) << hours << ":HOUR "
<< setw(2) << minutes << ":MIN :"
<< setw(2) << sec << "SEC";
return 0;
}