In: Computer Science
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds.
use unint and remainders for this and keep it as simple as possible.
#include <stdio.h>
int main()
{
//Declaring variables
int hrs,mins,secs,remainder;
int seconds;
char ch;
/* This while continues to execute until
* the user enters other than 'Y' or 'y'
*/
while(1)
{
//Getting the number of seconds by the user
printf("Enter the no of seconds :");
scanf("%d",&seconds);
//calculating the no of hours
hrs=seconds/3600;
remainder=seconds%3600;
//calculating the no of minutes
mins=remainder/60;
remainder=remainder%60;
//calculating the no of seconds
secs=remainder;
//Dispalying the output
printf("Hrs:Mins:Secs = %d:%d:%d",hrs,mins,secs);
//Prompting the user user to continue the program
again
printf("\nDo you Want to continue(Y/N):");
scanf(" %c",&ch);
if(ch=='y'||ch=='Y')
{
continue;
}else
{
printf("** Program Exit
**");
break;
}
}
return 0;
}
____________________
output:
________Thank You