Change Time Displayed 12 Hour Time Format Add Pm C Include Include Include Include Include Q37146217

Change the time being displayed to 12 hour time format and addam/pm in C.

#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>

unsigned int alarm(unsigned int seconds);
void timeout(int); // declare timeout() as a void function
int alrm_cnt = 0;
time_t current_time;
struct tm * time_info;
char timeString[9];

int main()
{
signal(SIGALRM, timeout);   // alarm timer routine
alarm(1);                  // set alarm timer for 1 second
/*** an infinite loop! ***/
while(1)
{
    pause(); // suspend process ’till signal
}

}
// asynchronously called function to handle programmed timeoutalarm
void timeout(int signo)
{
time(&current_time);
time_info = localtime(&current_time);
strftime(timeString, sizeof(timeString), “%H:%M:%S”,time_info);
puts(timeString);
signal(SIGALRM, timeout); // keep signal handler in tact
alarm(1);               // rearm process timer for next interval

}


Answer


//hr=hour

//min=minute

//sec=second

#include<stdio.h>

int main()

{

char time[10];

int hr,min,sec;

gets(time);

hr=(time[0]-‘0’)*10+(time[1]-‘0’);

min=(time[3]-‘0’)*10+(time[4]-‘0’);

sec=(time[6]-‘0’)*10+(time[7]-‘0’);

if(hr==12&&time[8]==’A’)

printf(“%02d:%02d:%02dn”,hr-12,min,sec);

else if(hr==12&&time[8]==’P’)

printf(“%02d:%02d:%02dn”,hr,min,sec);

else if(hr<12&&time[8]==’A’)

printf(“%02d:%02d:%02dn”,hr,min,sec);

if(hr<12&&time[8]==’P’)

printf(“%02d:%02d:%02dn”,hr+12,min,sec);

return 0;

}

//you must input the 12hr

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.