LearnFrenzy
Sign in Take a free test

Data Types · Data Type Programs · Question 4

Convert seconds into days, hours, minutes and seconds in C Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

Split a duration given in seconds into days, hours, minutes and seconds.

Solution
main.c
#include <stdio.h>

int main(void) {
    long total = 1000000;                   /* one million seconds */
    long days = total / 86400;
    long hours = total % 86400 / 3600;
    long minutes = total % 3600 / 60;
    long seconds = total % 60;
    printf("%ld s = %ld days %ld h %ld min %ld s\n", total, days, hours, minutes, seconds);
    return 0;
}

Output Verified by compiling and running

1000000 s = 11 days 13 h 46 min 40 s

Integer division gives the whole units and % the remainder, which feeds the next smaller unit. Here integer division is exactly what we want.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links