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

In summary, to display an arbitrary time using 2 ticks every 1 second, you will need to convert ticks into seconds, minutes, and hours using the / (integer division) and % (modulo) operators. For example, if you have 3602 seconds, you can divide it by 3600 seconds (1 hour in seconds) to get 1 hour, with a remainder of 2 seconds. Then, divide the remainder by 60 (1 minute in seconds) to get 0 minutes, with a remainder of 2 seconds. This will allow you to display the correct hours, minutes, and seconds.
  • #1
mathrocks
106
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
  • #2
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
 
  • #3
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:
  • #4
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
 

1. How does the C program display the current time?

The C program uses the built-in time() function to retrieve the current time from the system and then uses the localtime() function to convert it into a readable format.

2. How can I find the hour, minute, and second using this program?

The C program uses the struct tm data type to store the time values and then uses the tm_hour, tm_min, and tm_sec members to access the hour, minute, and second values respectively.

3. Can I change the format of the time displayed by the program?

Yes, you can use the strftime() function to customize the format of the time displayed. This function takes in a format string as an argument and returns the time in the specified format.

4. Is it possible to display the time in a different time zone?

Yes, you can use the setenv() function to set the desired time zone before calling the localtime() function. This will display the time according to the specified time zone.

5. How do I run this program on my computer?

To run this C program, you will need to have a C compiler installed on your computer. You can then compile the program using the command line or an IDE and run the executable file generated.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
933
  • Engineering and Comp Sci Homework Help
Replies
2
Views
922
  • Special and General Relativity
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top