Integer to Words | Practice | GeeksforGeeks

Write a function to convert a given number into words.
Example 1:
Input:
N = 438237764
Output: forty three crore eighty two lakh 
thirty seven thousand seven hundred and 
sixty four

 
Example 2:
Input:
N = 1000
Output: one thousand


Your Task:
You don't need to read input or print anything. Your task is to complete the function convertToWords() which takes the integer n as input parameters and returns a string containing n in words.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 109 - 1
 
+1
badgujarsachin832 weeks ago
string numtoword(long n,string s){
        string one[]={"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
    string tens[]={"","","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
        string str="";
        if(n>19){
            str+=tens[n/10]+one[n%10];
        }else{
            str+=one[n];
        }
        if(n!=0){
            str+=s;
        }
        return str;
    }
    string convertToWords(long n) {
        // code here
        string ans="";
        ans+=numtoword((int)(n/10000000),"crore ");
        ans+=numtoword((int)((n/100000)%100),"lakh ");
        ans+=numtoword((int)((n/1000)%100),"thousand ");
        ans+=numtoword((int)((n/100)%10),"hundred ");
        if(n>100 && n%100>0){
            ans+="and ";
        }
        ans+=numtoword((int)(n%100),"");
        return ans;
    }
-2
imranwahid2 weeks ago
Easy C++ solution
https://ide.geeksforgeeks.org/822974a8-b4b8-4d61-8553-2267dfe85fdb
-1
qwertyxcvzz4 weeks ago
class Solution {
    static String one[] = { "", "one ", "two ", "three ", "four ","five ", "six ", "seven ", "eight ","nine ", "ten ", "eleven ", "twelve ","thirteen ", "fourteen ", "fifteen ","sixteen ", "seventeen ", "eighteen ",
"nineteen " };
 
    static String ten[] = { "", "", "twenty ", "thirty ", "forty ","fifty ", "sixty ", "seventy ", "eighty ","ninety " };
    String numwords(int n,String s)
    {
        String str="";
        if(n>19)
        str+=ten[n/10]+one[n%10];
        else
        str+=one[n];
        
        if(n!=0)
         str+=s;
         
        return str; 
    }
    String convertToWords(long n) {
        String out="";
        
out+=numwords((int)(n/10000000),"crore");
out+=numwords((int)((n/100000)%100),"lakh");
out+=numwords((int)((n/1000)%100),"thousand");
out+=numwords((int)((n/100)%10),"hundred ");
if(n>100 && n%100>0)
   out+="and ";
         
    out+=numwords((int)(n%100), "");
      return out;
    }
}
0
shobhitchoudhary7453 months ago
string arr1[]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};       string arr2[]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};       int digit=log10(n)+1;       int ch=digit;       string crore="",lakh="",thousand="",hundred="",ten="",one="";       if(digit==9){           int i=n/100000000;            int j=(n/10000000)%10;           if(((i*10)+j)<20)           {               crore=arr1[((i*10)+j)-1]+" crore ";               digit-=2;               n-=(((i*10)+j)*10000000);           }           else           {               crore=arr2[i-2]+" ";               digit-=1;               n-=(i*100000000);           }       }       if(digit==8){           int c=0;           if(n<10000000){               crore+="crore ";           }           else{               c=1;               int i=n/10000000;               crore+=(arr1[i-1]+" crore ");               digit--;               n-=(i*10000000);           }if(c==0)           digit--;       }                     if(digit==7){           int c=0;           if(n>=1000000){               c=1;           int i=n/1000000;            int j=(n/100000)%10;           if(((i*10)+j)<20)           {               lakh=arr1[((i*10)+j)-1]+" lakh ";               digit-=2;               n-=(((i*10)+j)*100000);           }           else           {               lakh=arr2[i-2]+" ";               digit-=1;               n-=(i*1000000);           }           }           if(c==0)           digit--;       }       if(digit==6){           int c=0;           if(n<100000&&lakh!=""){               lakh+="lakh ";           }           else if(n>=100000){               int i=n/100000;               lakh+=(arr1[i-1]+" lakh ");               digit--;               n-=(i*100000);               c=1;           }           if(c==0)           digit--;       }       
       if(digit==5){           int c=0;           if(n>=10000){               c=1;           int i=n/10000;            int j=(n/1000)%10;           if(((i*10)+j)<20)           {               thousand=arr1[((i*10)+j)-1]+" thousand ";               digit-=2;               n-=(((i*10)+j)*1000);           }           else           {               thousand=arr2[i-2]+" ";               digit-=1;               n-=(i*10000);           }           }           if(c==0)           digit--;       }       if(digit==4){           int c=0;           if(n<1000&&thousand!=""){               thousand+="thousand ";           }           else if(n>=1000){               c=1;               int i=n/1000;               thousand+=(arr1[i-1]+" thousand ");               digit--;               n-=(i*1000);           }if(c==0)           digit--;       }              if(digit==3){           int c=0;           if(n>=100){               c=1;               int i=n/100;               hundred=arr1[i-1]+" hundred ";               digit--;               n-=(i*100);           }if(c==0)           digit--;       }       if(digit==2){       int c=0;           if(n>=10){               c=1;               int i=n/10;               int j=n%10;               if(((i*10)+j)<20){                   ten=arr1[((i*10)+j)-1]+" ";                   digit-=2;               }else{                   ten=arr2[i-2]+" ";                   digit--;                   n-=(i*10);                                  }           }if(c==0)           digit--;       }       if(digit==1){           if(n>=1)           one=arr1[n-1];       }              if(one!="" && ten==""&&ch>1){           string temp="and "+one;           one=temp;       }        else if(ten!=""&&ch>2){           string temp="and "+ten;           ten=temp;       }       
+2
wolfofsv5 months ago
Solve sub problem of converting a 2 digit int to word
treat hundred and and first
    string convert_2_dig(int n){
        return n < 20 ? ones[n] :tens[n/10] + ones[n%10];
    }
    
    string convertToWords(long n) {
        // code here
        string res = "";
        
        if(n%1000 >= 100){
            res += ones[(n%1000)/100] + "hundred ";
        }
        if((n/100 > 0) && (n%100 > 0)){
            res += "and ";
        }
        res += convert_2_dig(n%100);
        
        n /= 1000;
        int i = 0;
        while(n != 0){
            int num = n%100;
            string s = convert_2_dig(num);
            if(s.length() != 0){
                res = s + names[i] + res;
            }
            n /= 100;
            i++;
        }

        return res;
    }
	vector<string> ones = {"", "one ", "two ", "three ", "four ", 
						"five ", "six ", "seven ", "eight ", "nine ",
                        "ten ", "eleven ", "twelve ", "thirteen ", 
                        "fourteen ", "fifteen ", "sixteen ",
                        "seventeen ", "eighteen ", "nineteen "};
    vector<string> tens = {"", "", "twenty ", "thirty ", "forty ", 
    						"fifty ", "sixty ", "seventy ", "eighty ", 
    						"ninety "};
    vector<string> names = {"thousand ", "lakh ", "crore "};
+1
s37randive6 months ago
class Solution{   
private:
   string ones[20]={"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
   string tens[10]={"","","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
public:
   string numToWord(int n,string s){
       string str="";
       if(n>19){
           str+=tens[n/10]+ones[n%10];
       }else{
           str+=ones[n];
       }
       if(n){
           str+=s;
       }
       return str;
   }
   string convertToWords(long n) {
       string ans="";
       ans+=numToWord((n/10000000),"crore ");
       ans+=numToWord((n/100000)%100,"lakh ");
       ans+=numToWord((n/1000)%100,"thousand ");
       ans+=numToWord((n/100)%10,"hundred ");
       
       if(n>100 && n%100){
           ans+="and ";
       }
       ans+=numToWord(n%100,"");
       return ans;
   }
+1
ag14715886 months ago
Total time taken: 0.3 sec
 
class Solution {   String convertToWords(long n) {       // code here              String str = "";       for(int i=1; n>0; i++){                      // when "i" will be at 100th place            // it will get the digit at 100th place           if(i==2){               int m = (int)(n%10);               n/=10;                              // if string is empty 'and' will not be added to the string                // i.e. at 100,200               if(str.length()==0 ){                   str = numberToWord( m, i) + str;               }               else{                   str = numberToWord( m, i)+"and " + str;               }                          }           else{               int m = (int)(n%100);               n /=100;               str = numberToWord( m, i) + str;           }       }              if(str.charAt(str.length()-1) != ' '){           return str +" ";       }              return str;          }      public static String numberToWord( int num, int i){       if(num==0) return "";              // for number from 1 to 19       String[] ones = {"one", "two", "three", "four", "five", "six", "seven",                        "eight", "nine", "ten", "eleven", "twelve", "thirteen",                        "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",                        "nineteen"                       };               // for 20 ,30, ..., 90        String[] tens = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy",                           "eighty", "ninety"                       };              String[] zeros = { " hundred ", " thousand ", " lakh ", " crore "};              String rs = "";                        if( num<=19 ){                      rs = rs + ones[num-1];       }       else{               int m = num%10;               num /= 10;                              rs = tens[num-2] ;                              if(m>0){                   rs = rs+" "+ ones[m-1];               }         }              // here hundred ,thousand will be added to the string       // accourding to the value of 'i'        if( i>=2){           rs  = rs + zeros[i-2 ];       }
       return rs;   }}
 
 
 
+1
aloksinghbais026 months ago
C++ solution having time complexity as O(digits_in(N)) and space complexity as O(digits_in(N)) is as follows :-
 
Execution time -: 0.0 / 1.1 sec
 
class Solution{   public:
   string Unit(int pl){       if(pl == 8) return ("crore ");       if(pl == 6) return ("lakh ");       if(pl == 4) return ("thousand ");       if(pl == 3) return ("hundred ");       return "";   }      string F(int x){       if(x == 2) return ("twenty ");       if(x == 3) return ("thirty ");       if(x == 4) return ("forty ");       if(x == 5) return ("fifty ");       if(x == 6) return ("sixty ");       if(x == 7) return ("seventy ");       if(x == 8) return ("eighty ");       if(x == 9) return ("ninety ");   }      string S(int x){       if(x == 1) return ("one ");       if(x == 2) return ("two ");       if(x == 3) return ("three ");       if(x == 4) return ("four ");       if(x == 5) return ("five ");       if(x == 6) return ("six ");       if(x == 7) return ("seven ");       if(x == 8) return ("eight ");       if(x == 9) return ("nine ");       return "";   }      string anonymous(int f,int s){       if(s == 0) return ("ten ");       if(s == 1) return ("eleven ");       if(s == 2) return ("twelve ");       if(s == 3) return ("thirteen ");       if(s == 4) return ("fourteen ");       if(s == 5) return ("fifteen ");       if(s == 6) return ("sixteen ");       if(s == 7) return ("seventeen ");       if(s == 8) return ("eighteen ");       if(s == 9) return ("nineteen ");   }      string digitWordAndUnit(int f,int s,int pl){       string unit = Unit(pl);       if(pl == 1){           return S(s);       }       else if(pl == 2){           if(f == 0) return S(s);           else if(f == 1) return anonymous(f,s);           else return F(f) + S(s);       }       else if(pl == 3){           return S(s) + unit;       }       else{           if(f == 0){               return S(s) + unit;           }           else if(f == 1){               return anonymous(f,s) + unit;           }           else{               return F(f) + S(s) + unit;           }       }   }      string convertToWords(long n) {       int temp = n;       vector<int> v;       while(temp){           int x = temp % 10;           v.push_back(x);           temp /= 10;       }              if(v.size() == 1) return digitWordAndUnit(0,v[0],1);       else if(v.size() == 2) return digitWordAndUnit(v[1],v[0],2);              string ans{};       if(v.size() % 2){                      if(v.size() > 3){               int pl = v.size()-1;               while(pl>=4){                   if(v[pl] != 0 || v[pl-1] != 0){                       ans += digitWordAndUnit(v[pl],v[pl-1],pl);                   }                   pl -= 2;               }           }           if(v[2] != 0) ans += digitWordAndUnit(0,v[2],3);       }       else{           int pl = v.size();           ans += digitWordAndUnit(0,v[pl-1],pl);           pl -= 2;           while(pl >= 4){               if(v[pl] != 0 || v[pl-1] != 0){                   ans += digitWordAndUnit(v[pl],v[pl-1],pl);               }               pl -= 2;           }           if(v[2] != 0) ans += digitWordAndUnit(0,v[2],3);       }              if(v[1] != 0 || v[0] != 0){           ans += "and ";           ans += digitWordAndUnit(v[1],v[0],2);       }              return (ans);   }};
0
mayankkannojia296 months ago
JAVA .3 sec Big but best.
class Solution {   String convertToWords(long l) {       HashMap<Integer,String> hash = new HashMap<>();       hash.put(0,"");       hash.put(1,"one ");hash.put(2,"two ");hash.put(3,"three ");hash.put(4,"four ");       hash.put(5,"five ");hash.put(6,"six ");hash.put(7,"seven ");hash.put(8,"eight ");       hash.put(9,"nine ");hash.put(10,"ten ");hash.put(11,"eleven ");hash.put(12,"twelve ");       hash.put(13,"thirteen ");hash.put(14,"fourteen ");hash.put(15,"fifteen ");hash.put(16,"sixteen ");       hash.put(17,"seventeen ");hash.put(18,"eighteen ");hash.put(19,"nineteen ");       hash.put(20,"twenty ");hash.put(30,"thirty ");hash.put(40,"forty ");       hash.put(50,"fifty ");hash.put(60,"sixty ");hash.put(70,"seventy ");       hash.put(80,"eighty ");hash.put(90,"ninety ");hash.put(100,"hundred ");       hash.put(1000,"thousand ");hash.put(100000,"lakh ");hash.put(10000000,"crore ");
   String out = "";   int n = (int)l;   if(n>=10000000){       int c = n/10000000;       if(c>=20) out = out + hash.get(c-(c%10)) + hash.get(c%10) + "crore ";       else out = out + hash.get(c) + "crore ";       n= n%10000000;   }   if(n>=100000){       int c = n/100000;       if(c>=20) out = out + hash.get(c-(c%10)) + hash.get(c%10) + "lakh ";       else out = out + hash.get(c) + "lakh ";       n= n%100000;   }   if(n>=1000){       int c = n/1000;       if(c>=20) out = out + hash.get(c-(c%10)) + hash.get(c%10) + "thousand ";       else out = out + hash.get(c) + "thousand ";       n= n%1000;   }   if(n>=100){       int c = n/100;      out = out + hash.get(c) + "hundred ";      n=n%100;   }   if(n>=20 && (int)l>=100 ){        out = out + "and " + hash.get(n-(n%10)) + hash.get(n%10);   }  else if(n>0 && (int)l>=100 ) out = out +"and "+ hash.get(n);   else if((int)l<100){       if((int)l>=20)  out = out + hash.get(n-(n%10)) + hash.get(n%10);       else out = out + hash.get(n);   }   return out;}}
0
yashitanamdeo6 months ago
Python Solution 
#User function Template for python3

class Solution:
    def convertToWords(self, n):
        # code here
        
        one = ["", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", 
        "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ",
        "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
        
        ten = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
        
        def numToWords(n,s):
            
            str = ""
            
            if n > 19:
                str += ten[(n // 10)] + one[(n % 10)]
            else:
                str += one[n]
                
            if n:
                str += s
                
            return str
        
        output = ""
        
        output += numToWords((n // 10000000), "crore ")
        output += numToWords(((n // 100000) % 100), "lakh ")
        output += numToWords(((n // 1000) % 100), "thousand ")
        output += numToWords(((n // 100) % 10), "hundred ")
        
        if(n > 100 and n % 100):
            output += "and "
            
        output += numToWords((n % 100), "")
        
        return output

#{ 
#  Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    tc = int(input())
    while tc > 0:
        n = int(input())
        ob = Solution()
        ans = ob.convertToWords(n)
        print(ans)
        tc -= 1

# } Driver Code Ends
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.