This blog is under construction

Sunday 7 July 2013

C program to convert time in seconds to hours, minutes and seconds

Write a C program to convert time in seconds to hours, minutes and seconds.


  #include <stdio.h>
  #define HOURTOSEC 3600
  #define MINTOSEC 60

  int main() {
        int seconds, hour = 0, min = 0;

        /* get the input in seconds */
        printf("Enter your input for seconds:");
        scanf("%d", &seconds);

        printf("%d seconds = ", seconds);

        /* calculate number of hours */
        if (seconds >= HOURTOSEC) {
                while (1) {
                        hour++;
                        seconds = seconds - HOURTOSEC;
                        if (seconds < HOURTOSEC)
                                break;
                }
        }

        /* calculate no of minutes */
        if (seconds >= MINTOSEC) {
                while (1) {
                        min++;
                        seconds = seconds - MINTOSEC;
                        if (seconds < MINTOSEC)
                                break;
                }
        }

        /* print the output */
        printf("%d hours, %d minutes and %d seconds\n", hour, min, seconds);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input for seconds:7302
  7302 seconds = 2 hours, 1 minutes and 42 seconds





See Also:

No comments:

Post a Comment