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.
importjava.io.*;importjava.util.*;publicclassSolution{staticString[]digits={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};staticString[]tens={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};// public static void main(String[] args) {// /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */publicstaticvoidword(intnum){if(num/100>0){System.out.print(digits[num/100]+" Hundred ");}if((num%100)<20&&(num%100)>0){System.out.print(digits[num%100]+" ");}elseif((num/10)%10>0){System.out.print(tens[(num/10)%10]+" ");if(num%10>0){System.out.print(digits[num%10]+" ");}}}publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intt=scanner.nextInt();while(t-->0){longnum=scanner.nextLong();inttn=(int)(num/1000000000000L);intbn=(int)((num/1000000000L)%1000);intmn=(int)((num/1000000L)%1000);intth=(int)((num/1000L)%1000);inthd=(int)(num%1000);if((tn+bn+mn+th+hd)==0){System.out.print("Zero");}if(tn>0){word(tn);System.out.print("Trillion ");}if(bn>0){word(bn);System.out.print("Billion ");}if(mn>0){word(mn);System.out.print("Million ");}if(th>0){word(th);System.out.print("Thousand ");}if(hd>0){word(hd);}System.out.println();}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #17: Number to Words
You are viewing a single comment's thread. Return to all comments →