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
 
0
shobhitchoudhary7454 weeks 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
wolfofsv2 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 "};
+2
s37randive3 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
ag14715883 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;   }}
 
 
 
0
aloksinghbais023 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
mayankkannojia293 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
yashitanamdeo4 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
0
gaurawk784 months ago
#include <iostream>
using namespace std;
// strings at index 0 is not used, it is to make array
// indexing simple
string one[] = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen "
};
// strings at index 0 and 1 are not used, they is to
// make array indexing simple
string ten[] = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety "
};
// n is 1- or 2-digit number
string numToWords(int n, string s)
{
string str = "";
// if n is more than 19, divide it
if (n > 19)
str += ten[n / 10] + one[n % 10];
else
str += one[n];
// if n is non-zero
if (n)
str += s;
return str;
}
// Function to print a given number in words
string convertToWords(long n)
{
// stores word representation of given number n
string out;
// handles digits at ten millions and hundred
// millions places (if any)
out += numToWords((n / 10000000), "crore ");
// handles digits at hundred thousands and one
// millions places (if any)
out += numToWords(((n / 100000) % 100), "lakh ");
// handles digits at thousands and tens thousands
// places (if any)
out += numToWords(((n / 1000) % 100), "thousand ");
// handles digit at hundreds places (if any)
out += numToWords(((n / 100) % 10), "hundred ");
if (n > 100 && n % 100)
out += "and ";
// handles digits at ones and tens places (if any)
out += numToWords((n % 100), "");
return out;
}
// Driver code
int main()
{
// long handles upto 9 digit no
// change to unsigned long long int to
// handle more digit number
long n = 438237764;
// convert given number in words
cout << convertToWords(n) << endl;
return 0;
}
+2
kritipashine004 months ago
C++ Solution
class Solution{   
public:
    string convertToWords(long n) {
        // code here
        string ones[10]={"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
        string tens1[10]={"ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
        string tens2[10]={"","","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
        string bigger[4]={"hundred","thousand ","lakh ","crore "};
        int i=0;
        string ans="";
        int f=0,hundred;
        while(n>0){
            string s1="";
            int one=n%10;
            n/=10;
            int ten=n%10;
            n/=10;
            if(ten==0){
                s1=ones[one];
            }
            else{
                if(ten==1){
                    s1=tens1[one];
                }
                else{
                    s1=tens2[ten]+ones[one];
                }
            }
            if(i==0){
                hundred=n%10;
                n=n/10;
            }
            if(i==0 && hundred!=0){
                if(s1.size()>1)s1=ones[hundred]+"hundred and "+s1;
                else{
                    s1=ones[hundred]+"hundred ";
                }
                f=1;
             }
            else if(i>=1 and s1.size()>1){
                if(f==1)s1=s1+bigger[i];
                else{
                    s1=s1+bigger[i]+"and ";
                    f=1;
                }
            }
            ans=s1+ans;
            i++;
            
        }
        
        return ans;
    }
};
0
kritipashine00
This comment was deleted.
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.