Program to convert a given number to words | Set 2

Write code to convert a given number into words.

Examples:

Input: 438237764
Output: forty three crore eighty two lakh thirty
        seven thousand seven hundred and sixty four 

Input: 999999
Output: nine lakh ninety nine thousand nine hundred 
        and ninety nine

Input: 1000
Output: one thousand 

We have already discussed a approach that handles numbers from 0 to 9999 in previous post. In this post, another approach is discussed which can handles number till 20-digits long which are less than ULLONG_MAX (Maximum value for an object of type unsigned long long int). ULLONG_MAX is equal to 18446744073709551615 in decimal assuming compiler takes 8 bytes for storage of unsigned long long int.

Below representation shows place value chart for any 9 digit positive integer –

4  3  8  2  3  7  7  6  4
|  |  |  |  |  |  |  |  |__ ones' place
|  |  |  |  |  |  |  |__ __ tens' place
|  |  |  |  |  |  |__ __ __ hundreds' place
|  |  |  |  |  |__ __ __ __ thousands' place
|  |  |  |  |__ __ __ __ __ tens thousands' place
|  |  |  |__ __ __ __ __ __ hundred thousands' place
|  |  |__ __ __ __ __ __ __ one millions' place
|  |__ __ __ __ __ __ __ __ ten millions' place
|__ __ __ __ __ __ __ __ __ hundred millions' place

The idea is to divide the number into individual digits based on above place value chart and handle them starting from Most significant digit. Here’s simple implementation that supports numbers having maximum 9 digits. Program can be easily extended to support any 20-digit number.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C++ program to print a given number in words.
   The program handles till 9 digits numbers and
   can be easily extended to 20 digit number */
#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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java program to print a given number in words. 
The program handles till 9 digits numbers and 
can be easily extended to 20 digit number */
class GFG {
  
// Strings at index 0 is not used, it is to make array 
// indexing simple 
    static 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 
    static String ten[] = {"", "", "twenty ", "thirty ", "forty ",
        "fifty ", "sixty ", "seventy ", "eighty ",
        "ninety "
    };
  
// n is 1- or 2-digit number 
    static 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 != 0) {
            str += s;
        }
  
        return str;
    }
  
// Function to print a given number in words 
    static 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((int) (n / 10000000), "crore ");
  
        // handles digits at hundred thousands and one 
        // millions places (if any) 
        out += numToWords((int) ((n / 100000) % 100), "lakh ");
  
        // handles digits at thousands and tens thousands 
        // places (if any) 
        out += numToWords((int) ((n / 1000) % 100), "thousand ");
  
        // handles digit at hundreds places (if any) 
        out += numToWords((int) ((n / 100) % 10), "hundred ");
  
        if (n > 100 && n % 100 > 0) {
            out += "and ";
        }
  
        // handles digits at ones and tens places (if any) 
        out += numToWords((int) (n % 100), "");
  
        return out;
    }
  
// Driver code 
    public static void main(String[] args) {
        // 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 
        System.out.printf(convertToWords(n));
  
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print a given number in words.
# The program handles till 9 digits numbers and
# can be easily extended to 20 digit number 
  
# strings at index 0 is not used, it 
# is to make array indexing simple
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
ten = [ "", "", "twenty ", "thirty ", "forty ",
        "fifty ", "sixty ", "seventy ", "eighty ",
        "ninety "];
  
# n is 1- or 2-digit number
def numToWords(n, s):
  
    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
def convertToWords(n):
  
    # stores word representation of given 
    # number n
    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 and n % 100):
        out += "and ";
  
    # handles digits at ones and tens
    # places (if any)
    out += numToWords((n % 100), "");
  
    return out;
  
# Driver code
  
# long handles upto 9 digit no
# change to unsigned long long 
# int to handle more digit number
n = 438237764;
  
# convert given number in words
print(convertToWords(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C# program to print a given number in words. 
The program handles till 9 digits numbers and 
can be easily extended to 20 digit number */
using System;
class GFG 
{
  
// strings at index 0 is not used, it is 
// to make array indexing simple 
static 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 
static string []ten = {"", "", "twenty ", "thirty ", "forty ",
                       "fifty ", "sixty ", "seventy ", "eighty ",
                       "ninety "};
  
// n is 1- or 2-digit number 
static 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 != 0) 
    {
        str += s;
    }
  
    return str;
}
  
// Function to print a given number in words 
static string convertToWords(long n)
{
      
    // stores word representation of 
    // given number n 
    string out1 = "";
  
    // handles digits at ten millions and 
    // hundred millions places (if any) 
    out1 += numToWords((int) (n / 10000000), 
                                  "crore ");
  
    // handles digits at hundred thousands 
    // and one millions places (if any) 
    out1 += numToWords((int) ((n / 100000) % 100), 
                                         "lakh ");
  
    // handles digits at thousands and tens  
    // thousands places (if any) 
    out1 += numToWords((int) ((n / 1000) % 100), 
                                   "thousand ");
  
    // handles digit at hundreds places (if any) 
    out1 += numToWords((int) ((n / 100) % 10), 
                                  "hundred ");
  
    if (n > 100 && n % 100 > 0) 
    {
        out1 += "and ";
    }
  
    // handles digits at ones and tens 
    // places (if any) 
    out1 += numToWords((int) (n % 100), "");
  
    return out1;
}
  
// Driver code 
static void 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 
    Console.WriteLine(convertToWords(n));
}
}
  
// This code is contributed by mits

chevron_right


PHP

19)
{
$str .= $ten[(int)($n / 10)];
$str .= $one[$n % 10];
}
else
$str .= $one[$n];

// if n is non-zero
if ($n != 0 )
$str .= $s;

return $str;
}

// Function to print a given number in words
function convertToWords($n)
{
// stores word representation of
// given number n
$out = “”;

// handles digits at ten millions and
// hundred millions places (if any)
$out .= numToWords((int)($n / 10000000), “crore “);

// handles digits at hundred thousands
// and one millions places (if any)
$out .= numToWords(((int)($n / 100000) % 100), “lakh “);

// handles digits at thousands and tens
// thousands places (if any)
$out .= numToWords(((int)($n / 1000) % 100), “thousand “);

// handles digit at hundreds places (if any)
$out .= numToWords(((int)($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

// long handles upto 9 digit no
// change to unsigned long long int to
// handle more digit number
$n = 438237764;

// convert given number in words
echo convertToWords($n) . “\n”;

// This code is contributed by Akanksha Rai
?>


Output:

forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four 

Time complexity of above solution is O(1).
Auxiliary space used by the program is O(1).

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.