In: Computer Science
In C program
#include<stdio.h>
Write a program that prompts the user to input the elapsed time for an event in seconds.
The program then outputs the elapsed time in hours, minutes, and seconds.
Example (Numbers with underscore indicate an input):
Enter the elapsed time in seconds: 9630
The elapsed time in seconds = 9630
The equivalent time in hours:minutes:seconds = 02:40:30
HINT: Pay attention to the printf format descriptors.
Solution for the given question are as follows -
Code :
#include <stdio.h>
int main() {
// local variable declaration
int esec;
// get input from user
printf("Enter the elapsed time in seconds: ");
scanf("%d", &esec);
// calculate hours, minutes, seconds
int hr = (esec/3600);
int min = (esec -(3600*hr))/60;
int sec = (esec -(3600*hr)-(min*60));
// print the output
printf("\nThe elapsed time in seconds = %d",
esec);
printf("\nThe equivalent time in hours:minutes:seconds
= %d:%d:%d\n",hr,min,sec);
return 0;
}
Code Screen Shot :
Output :