Time Conversion Hackerrank Solution in C Program
ADVERTISEMENTS
Time conversion hackerrank solution in C program.
In this article, you will learn how to make a time conversion hackerrank solution in C program.
Example
INPUT TIME::
08:06:59PM
CONVERTED TIME:: 20:06:59
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function
Source Code
// Time Conversion Hackerrank Solution in C Program
#include <stdio.h>
int main() {
char timestamp[11]="";
int hr=0;
printf("INPUT TIME::\n");
scanf("%s", timestamp);
if (timestamp[8]=='P') {
hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
if (hr < 12) hr += 12;
}
else {
hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
if (hr==12) hr=0;
}
timestamp[0] = hr/10 + '0';
timestamp[1] = hr%10 + '0';
timestamp[8] = '\0';
timestamp[9] = '\0';
// It will print the final output
printf("\nCONVERTED TIME:: %s", timestamp);
return 0;
}
Output
INPUT TIME::
08:06:59PM
CONVERTED TIME:: 20:06:59
Explanation
In this given program, we have taken input 08:06:59PM
from the user then we make calculations on this input to find the time format. Then It will return the 20:06:59
the output of the program.