2020 Updated Time Conversion Hackerrank Solution in by Coding Demons


For better understanding refer  YouTube Tutorial Video 


CODE:

/***SOLUTION BY CODING DEMONS***/

#include<bits/stdc++.h>
using namespace std;

int main(){

int n;      //for hour
string s;    //for rest
cin>>n;   
cin>>s;   

//AM or PM is present at 6th position
if(s[6]=='P'){      
                                         
if(n==12){   //for PM
 cout<<n; 
}     
else {   

//to convert in 24 hr format
 n=n+12;
 cout<<n; 
}  
}     
else {     //for AM
if(n==12){
cout<<"00";
}  
else {  
cout<<"0"<<n;
//as n will print single digit only
}        
}   
    
//no need to print AM/PM in 24hr format
s.erase(6); 
                       
cout<<s;                                      //print the remaining string

return 0;
}

/***********END***********/




!! SUBSCRIBE FOR MORE CODES AND VIDEOS !!

Post a Comment

0 Comments