C Program to Display Time: Find Hour, Minute & Second

  • Thread starter Thread starter mathrocks
  • Start date Start date
  • Tags Tags
    Program Time
Click For Summary
SUMMARY

The discussion focuses on creating a C program to convert ticks into hours, minutes, and seconds. The conversion process involves using integer division (/) and the modulo operator (%) to accurately calculate the time components. Specifically, for an input of ticks, seconds are derived by multiplying ticks by 2, followed by calculating minutes and hours through successive divisions and remainders. The correct implementation ensures accurate representation of time, as demonstrated with the example of 3602 seconds resulting in 1 hour, 0 minutes, and 2 seconds.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with integer division and modulo operations
  • Basic knowledge of time measurement (seconds, minutes, hours)
  • Ability to manipulate and display output in C
NEXT STEPS
  • Learn about C programming data types and their usage
  • Explore advanced C operators, specifically integer division and modulo
  • Research time conversion algorithms in programming
  • Practice displaying formatted output in C using printf()
USEFUL FOR

C programmers, students learning time conversion algorithms, and anyone interested in improving their skills in manipulating integers and displaying formatted output in C.

mathrocks
Messages
105
Reaction score
0
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...
 
Physics news on Phys.org
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
 
chroot said:
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
Yes, that's correct. So if I do the following
seconds=ticks*2
minutes=seconds/60
hours=minutes/60

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:
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
566
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
7
Views
3K