top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C program to convert date from 24 hrs format to 12 hrs format?

0 votes
488 views

Write a C program to convert date from 24 hrs format to 12 hrd format?
Ex: 23:10 = 11:10PM

posted Sep 27, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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;

}

answer Oct 2, 2016 by Devendra Bohre
...