Convert seconds into hours, minutes, seconds
Convert seconds into hours, minutes, seconds
Write a C program to convert a given integer (in seconds) to hours, minutes and seconds.
Pictorial Presentation:
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:
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.
Go to:
PREV : Break amount into smallest banknotes.
NEXT : Convert days to years, months, days.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.