w3resource

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

C Basic Declarations and Expressions: Exercise-17 with Solution

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

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-17.php