Calculate an uptime percentage
Problem statement
Uptime is usually reported as a percentage: how many seconds a service was actually up, out of the total seconds it was supposed to be running. The formula is one line: (uptime_seconds / total_seconds) * 100.
Common mistake: dividing two whole numbers in Java or C++ and losing the decimal part before the multiplication even happens.
Wrong (Java/C++): int percent = (uptimeSeconds / totalSeconds) * 100;If uptimeSeconds and totalSeconds are both int, the division happens first and gets rounded down to a whole number before it's ever multiplied by 100, usually landing on 0. Using double for both values keeps the decimal through the whole calculation.
Your task: given uptimeSeconds = 86000 and totalSeconds = 86400, print the uptime percentage rounded to 2 decimal places.
Expected output:
99.54Approach
This is a single expression, no branching needed, just the formula followed by a rounding step:
uptime_seconds = 86000
total_seconds = 86400
percentage = (uptime_seconds / total_seconds) * 100
print(round(percentage, 2))