hello everyone,I must solve a problem which effect my c programming lesson grade...Here is the problem:
Write a program to convert a time given seconds to hours,minutes and seconds format.

Sample Run:
Enter seconds: 8230
It is 2 hours, 17 minutes ,10 seconds…

We learned only #include<stdio.h> not iostream or other.Thanks in advance.

Recommended Answers

All 3 Replies

You can use the division operator / to extract the number of hours and minutes. How many seconds in an hour? How many in one minute?

8230 / (60 * 60) = number of hours

Yeah, but this is a C++ forum. :)
Little help: 1 hour = 60*60 seconds = 3600secs.

First you have to store the amount of seconds that can't be converted to hours.
8230 % 3600 = 1030. You store the reminder in a temporary variable, then divide 8230 by 3600.
8230 / 3600 = 2 hours( int ).
Now you have to convert the remaining seconds (1030) to minutes. You store the remaining seconds that can't be converted to minutes in a temporary variable. 1030 % 60 = 10. Now calculate the minutes: 1030 / 60 = 17.

Put all it together and your time will be 2 hours 17 minutes 10 seconds. You can check the result by adding them together. 2*3600 + 17*60 + 10 = 7200 + 1020 +10 = 8230

thanks a lot guys,they'll benefit my work:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.