Number to word conversion
Given a positive number n, convert it into words. Assume that n <= 10000000000.
For example,
Output: Twenty One Billion, Fifty Two Crore, Twenty Three Lakh, Twenty Nine Thousand Nine Hundred and One
Input: 14632
Output: Fourteen Thousand Six Hundred and Thirty Two
The following diagram shows a place value chart for a 12-digit positive number:

We can easily solve this problem by splitting the given number into individual digits based on the above place value chart starting from the least significant digit, as shown below in C++, Java, and Python:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#include <iostream> #include <string> using namespace std; const string EMPTY = ""; const string X[] = { EMPTY, "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " }; const string Y[] = { EMPTY, EMPTY, "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " }; // Function to convert a single-digit or two-digit number into words string convertToDigit(unsigned long long n, string suffix) { // if `n` is zero if (n == 0) { return EMPTY; } // split `n` if it is more than 19 if (n > 19) { return Y[n / 10] + X[n % 10] + suffix; } else { return X[n] + suffix; } } // Function to convert a given number (max 9-digits) into words string convert(unsigned long long n) { // string to store word representation of the given number string res; // this handles digits at ones and tens place res = convertToDigit((n % 100), ""); if (n > 100 && n % 100) { res = "and " + res; } // this handles digit at hundred place res = convertToDigit(((n / 100) % 10), "Hundred ") + res; // this handles digits at thousand and tens thousand place res = convertToDigit(((n / 1000) % 100), "Thousand, ") + res; // this handles digits at hundred thousand and one million place res = convertToDigit(((n / 100000) % 100), "Lakh, ") + res; // this handles digits at ten million and hundred million place res = convertToDigit((n / 10000000) % 100, "Crore, ") + res; // this handles digits at ten million and hundred million place res = convertToDigit((n / 1000000000) % 100, "Billion, ") + res; // replace ", and" by " and" size_t pos; while ((pos = res.find(", and")) != string::npos) { res.replace(pos, 1, ""); } res.pop_back(); // remove trailing space if (res[res.size()-1] == ',') { res.pop_back(); // remove trailing comma } return res; } // C++ program to convert numbers to words int main() { cout << convert(99) << endl; cout << convert(1000) << endl; cout << convert(14632) << endl; cout << convert(997751076) << endl; cout << convert(2147483647) << endl; return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
class Main { private static final String EMPTY = ""; private static final String[] X = { EMPTY, "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ","Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " }; private static final String[] Y = { EMPTY, EMPTY, "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " }; // Function to convert a single-digit or two-digit number Longo words private static String convertToDigit(long n, String suffix) { // if `n` is zero if (n == 0) { return EMPTY; } // split `n` if it is more than 19 if (n > 19) { return Y[(int) (n / 10)] + X[(int) (n % 10)] + suffix; } else { return X[(int) n] + suffix; } } // Function to convert a given number (max 9-digits) Longo words public static String convert(long n) { // for storing the word representation of the given number StringBuilder res = new StringBuilder(); // add digits at ten million and hundred million place res.append(convertToDigit((n / 1000000000) % 100, "Billion, ")); // add digits at ten million and hundred million place res.append(convertToDigit((n / 10000000) % 100, "Crore, ")); // add digits at hundred thousand and one million place res.append(convertToDigit(((n / 100000) % 100), "Lakh, ")); // add digits at thousand and tens thousand place res.append(convertToDigit(((n / 1000) % 100), "Thousand, ")); // add digit at hundred place res.append(convertToDigit(((n / 100) % 10), "Hundred ")); if ((n > 100) && (n % 100 != 0)) { res.append("and "); } // add digits at ones and tens place res.append(convertToDigit((n % 100), "")); return res.toString().trim() .replace(", and", " and") .replaceAll("^(.*),$", "$1"); // remove trailing comma } // Java program to convert numbers to words public static void main(String[] args) { System.out.println(convert(99L)); System.out.println(convert(1000L)); System.out.println(convert(14632L)); System.out.println(convert(997751076L)); System.out.println(convert(2147483647L)); } } |
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# Function to convert a single-digit or two-digit number into words def convertToDigit(n, suffix): # if `n` is zero if n == 0: return EMPTY # split `n` if it is more than 19 if n > 19: return Y[n // 10] + X[n % 10] + suffix else: return X[n] + suffix # Function to convert a given number (max 9-digits) into words def convert(n): # add digits at ten million and hundred million place result = convertToDigit((n // 1000000000) % 100, 'Billion, ') # add digits at ten million and hundred million place result += convertToDigit((n // 10000000) % 100, 'Crore, ') # add digits at hundred thousand and one million place result += convertToDigit(((n // 100000) % 100), 'Lakh, ') # add digits at thousand and tens thousand place result += convertToDigit(((n // 1000) % 100), 'Thousand, ') # add digit at hundred place result += convertToDigit(((n // 100) % 10), 'Hundred ') if n > 100 and n % 100: result += 'and ' # add digits at ones and tens place result += convertToDigit((n % 100), '') return result.strip().rstrip(',').replace(', and', ' and') # Python program to convert numbers to words if __name__ == '__main__': EMPTY = '' X = [EMPTY, 'One ', 'Two ', 'Three ', 'Four ', 'Five ', 'Six ', 'Seven ', 'Eight ', 'Nine ', 'Ten ', 'Eleven ', 'Twelve ', 'Thirteen ', 'Fourteen ', 'Fifteen ', 'Sixteen ', 'Seventeen ', 'Eighteen ', 'Nineteen '] Y = [EMPTY, EMPTY, 'Twenty ', 'Thirty ', 'Forty ', 'Fifty ', 'Sixty ', 'Seventy ', 'Eighty ', 'Ninety '] print(convert(99)) print(convert(1000)) print(convert(14632)) print(convert(997751076)) print(convert(2147483647)) |
Ninety Nine
One Thousand
Fourteen Thousand, Six Hundred and Thirty Two
Ninety Nine Crore, Seventy Seven Lakh, Fifty One Thousand and Seventy Six
Two Billion, Fourteen Crore, Seventy Four Lakh, Eighty Three Thousand, Six Hundred and Forty Seven
The time complexity of the above solution is constant. We can easily extend the C++ program to handle numbers till 20-digits long, less than ULLONG_MAX (The maximum value for an object of type unsigned long long int). An ULLONG_MAX is equal to 18446744073709551615 in decimal, assuming that the compiler takes 8 bytes for its storage.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)