Time Conversion - HackerRank || HackerRank Time Conversion Solution

Problem Name: Time Conversion
Judge: HackerRank
Problem link: Time Conversion



HackerRank Time Conversion Solution in C

#include <stdio.h>
#include <string.h>
int main()
{
    int hh, mm, ss;
    char t12[3];
    scanf("%d:%d:%d%s", &hh, &mm, &ss, &t12);
    if (strcmp(t12,"PM") == 0 && hh != 12) hh += 12;
    if (strcmp(t12,"AM") == 0 && hh == 12) hh = 0;
    printf("%02d:%02d:%02d\n", hh, mm, ss);
    return 0;
}


HackerRank Time Conversion Solution in C++/Cpp

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int hh, mm, ss;
    char t12[3];
    scanf("%d:%d:%d%s", &hh, &mm, &ss, &t12);
    if (strcmp(t12,"PM") == 0 && hh != 12) hh += 12;
    if (strcmp(t12,"AM") == 0 && hh == 12) hh = 0;
    printf("%02d:%02d:%02d\n", hh, mm, ss);
    return 0;
}


HackerRank Time Conversion Solution in Java

import java.util.Scanner;

public class Time_Conversion {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String time = in.next();
        String[] listTime = time.split(":");
        String hour = listTime[0];
        String minutes = listTime[1];
        String seconds = listTime[2].substring(02);
        String caser = listTime[2].substring(24);
        if (caser.equals("AM")) {
            if (hour.equals("12"))
                hour = "00";
            System.out.println(hour + ":" + minutes + ":" + seconds);
        } else {
            if (!hour.equals("12")) {
                int h = Integer.parseInt(hour);
                h = h + 12;
                hour = "" + h;
            }
            System.out.println(hour + ":" + minutes + ":" + seconds);
        }
    }
}




Next Post Previous Post
No Comment
Add Comment
comment url