We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstringMethod(inth,intm){stringresult="";varhour=Converter(h);varminutes=Converter(m);vars=m>1?"s":"";if(m==0){returnresult=$"{hour} {minutes}";}if(minutes=="half"||minutes=="quarter"){switch(m){case<=30:result=$"{minutes} past {hour}";break;default:hour=Converter(h+1);result=$"{minutes} to {hour}";break;}}else{switch(m){case<=30:result=$"{minutes} minute{s} past {hour}";break;default:hour=Converter(h+1);result=$"{minutes} minute{s} to {hour}";break;}}returnresult;}publicstringConverter(intnumber){stringresult="";string[]words=newstring[]{"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};string[]fractions=newstring[]{"o' clock","quarter","half"};switch(number){case0:result=fractions[0];returnresult;case15:case45:result=fractions[1];returnresult;case30:result=fractions[2];returnresult;case<=20:// hoursresult=words[number];returnresult;default:varwholeNumber="";varunitNumber=0;// for minutes lower then 30if(number<30){wholeNumber=number.ToString();unitNumber=int.Parse(wholeNumber.Substring(1,1));result=$"{words[20]} {words[unitNumber]}";returnresult;}// for minutes past 30varsum=60-number;if(sum<=20){result=words[sum];returnresult;}elseif(sum<30){wholeNumber=sum.ToString();unitNumber=int.Parse(wholeNumber.Substring(1,1));result=$"{words[20]} {words[unitNumber]}";}returnresult;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Time in Words
You are viewing a single comment's thread. Return to all comments →
C#