Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C program display time

  1. Oct 1, 2006 #1
    Ok, I have to write a program that displays an arbitary time. I'm given 2 ticks every 1 second. So I'm suppose to use that to display the Hour, minute and seconds...im not sure where to start on how to create this. I know to find seconds I just do ticks/2, but for minutes and hours I'm confused...
  2. jcsd
  3. Oct 1, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    So your program is given an integer -- represented in ticks -- and you just need to convert ticks into seconds, minutes, hours, etc?

    I'd suggest you look into using the / (integer division) and % (modulo) operators.

    - Warren
  4. Oct 1, 2006 #3
    Yes, that's correct. So if I do the following

    how do I then use those values to display the correct hours, minutes?

    for example I have 3602 seconds, that will be 60.0333333 minutes, and 1.00055556 hours
    Last edited: Oct 1, 2006
  5. Oct 1, 2006 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You're not understanding me. The best solution uses BOTH the % and / operators.

    If you divide 3602 seconds by 3600 seconds (1 hour in seconds) with integer division, the answer will be 1. This is the number of hours.

    If you then perform the same divison, but take the remainder (using the % operator), the remainder is 2.

    Perform the integer division 2 / 60 (1 minute in seconds). The answer will be 0. This is the number of minutes.

    Perform the same division, but keep the remainder (2 % 60), which will be 2. This is the number of seconds.

    - Warren
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Similar Discussions: C program display time
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)