# Thread: 24-hour time to 12-hour time

1. ## 24-hour time to 12-hour time

OK I got this so far:

Code:
```#include <stdio.h>

main ()
{

int tfhour;
int tfseconds;
int twhour;

printf("Enter a 24-hour time (ex. 10:23): ");
scanf("%d:%d", &tfhour, &tfseconds);

if (tfseconds > 60 || tfseconds < 0)
{
printf("Not a valid seconds time!\n");
return;
}

if (tfhour <= 12 && tfhour >= 0)
printf("Equivalent 12-hour time: %d:%.2d AM\n", tfhour, tfseconds);

else if (tfhour >= 13 && tfhour <= 24)
{
twhour = (tfhour - 12);
printf("Equivalent 12-hour time: %d:%d PM\n", twhour, tfseconds);
}

else
printf("Not a valid hour!\n");
return;

}```
I got two questions!
1) I need to get two zeros on the seconds output instead of a single digit. I know why it is doing it..lol But I need to know how to fix it?

2) As far as military time is concerned 12:00 is 12:00 pm while 24:00 is 12:00 AM. How can I get that to work out like that? Another if statement maybe?

Any help would be appreciated!

2. You've got a minor issue in your first scanf: You never scan for seconds, just the hour and seconds.

Otherwise, you've already got it started right, use "%02d" for displaying your two numbers.

Quzah.

3. ## Re: 24-hour time to 12-hour time

Code:
```#include <stdio.h>

main ()
{

int tfhour;
int tfseconds;
int twhour;

printf("Enter a 24-hour time (ex. 10:23): ");
scanf("%d:%d", &tfhour, &tfseconds);

if (tfseconds > 60 || tfseconds < 0)
{
printf("Not a valid seconds time!\n");
return;
}

if(tfhour<0 || tfhour>24) printf("Not a valid hour!\n");
else {
printf("Equivalent 12-hour time: %d:%.2d ", tfhour, tfseconds);
if(tfhour/12 == 1) printf("PM\n"); else printf("AM\n");
}
return;

}```

4. Code:
``` if (hour > 12)
{
hour -= 12;
pm = 1;
}```
sorted.

5. OK after reading through everyones suggestions I came up w/ this:
Code:
```#include <stdio.h>

main ()
{

int tfhour;
int tfseconds;
int twhour;

printf("Enter a 24-hour time (ex. 10:23): ");
scanf("%d:%d", &tfhour, &tfseconds);

if (tfseconds > 60 || tfseconds < 0)
{
printf("Not a valid seconds time!\n");
return;
}

if(tfhour <= 0 || tfhour > 24)
{
printf("Not a valid hour!\n");
return;
}

else if (tfhour <= 12 && tfhour > 0)
twhour = tfhour;

else if (tfhour >= 13 && tfhour <= 24)
twhour = (tfhour - 12);

printf("Equivalent 12-hour time: %d:%.2d ", twhour, tfseconds);

if (tfhour / 12 == 1)
printf("PM\n");

else
printf("AM\n");

return;
}```
It seems to work, but seeing if maybe the experts can find something else wrong w/ it?...lol I'm not looking for effiecent here...just making sure it works!

6. To make it more efficient you could replace that:
Code:
```else if (tfhour <= 12 && tfhour > 0)
twhour = tfhour;

else if (tfhour >= 13 && tfhour <= 24)
twhour = (tfhour - 12);```
with that:

Code:
`twhour=(tfhour-1)%12+1;`

7. Just as a note, 00:00 is valid to use instead of 24:00

8. >As far as military time is concerned 12:00 is 12:00 pm while 24:00 is 12:00 AM.

Isn't 00:00 the equivalent of 12:00 AM? And 00:30 would be valid but 24:15 invalid?

>main ()

FAQ

>To make it more efficient you could replace [...]

Efficiency is difficult to generalize. I have seen a one-liner that took orders of magnitude more time than its multiline counterpart that used temporary variables. In this example, if I were interested in (speed) efficiency, I might be a little more shy about doing a division because my typical implementation would call a library routine. YMMV.

9. You might try for efficiency/readability:

Code:
```    . . . .
else if (tfhour <= 12 && tfhour > 0)
{
twhour = tfhour;
ampm = 0;
}
else if (tfhour >= 13 && tfhour <= 24)
{
twhour = (tfhour - 12);
ampm = 1;
}

printf("Equivalent 12-hour time: %d:%.2d ", twhour, tfseconds);

if (ampm)  printf("PM\n");
else   printf("AM\n");

return;
}```
Also, why hours and seconds? I've always used hours and minutes

Popular pages Recent additions