w3resource

Convert seconds into hours, minutes, seconds


C Practice Exercise

Write a C program to convert a given integer (in seconds) to hours, minutes and seconds.

Pictorial Presentation:

C Programming: Convert a given integer to hours, minutes and seconds

C Code:

#include <stdio.h>
int main() {
    int sec, h, m, s; // Declare variables for seconds, hours, minutes, and seconds
    
    // Prompt user for input seconds and store in 'sec'
    printf("Input seconds: ");
    scanf("%d", &sec);
	
    // Calculate hours, minutes, and remaining seconds
    h = (sec/3600); 
    m = (sec -(3600*h))/60;
    s = (sec -(3600*h)-(m*60));
	
    // Print the time in format H:M:S
    printf("H:M:S - %d:%d:%d\n",h,m,s);
	
    return 0;
}

Sample Output:

Input seconds: 25300                                                   
H:M:S - 7:1:40

Flowchart:

C Programming Flowchart: Convert a given integer to hours, minutes and seconds

For more Practice: Solve these Related Problems:

  • Write a C program to convert a given number of seconds into days, hours, minutes, and seconds.
  • Write a C program to convert a total of milliseconds into hours, minutes, seconds, and remaining milliseconds.
  • Write a C program to convert seconds into time format (HH:MM:SS) with leading zeros for single-digit values.
  • Write a C program to convert seconds into hours, minutes, and seconds, then adjust the time based on a given time zone offset.

C Programming Code Editor:



Previous: Write a C program to read an amount (integer value) and break the amount into smallest possible number of bank notes.
Next: Write a C program to convert a given integer (in days) to years, months and days, assumes that all months have 30 days and all years have 365 days.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.