Open In App

Program to convert a given number to words

Last Updated : 29 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to convert the given number into words.

Examples :

Input: N = 438237764
Output: Four Hundred Thirty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty Four

Input: N = 1000
Output: One Thousand

Approach: This approach is valid for converting number into words for up to 20 digits, and is basically based on Brute Force Logic.

The idea is to break down the number into International Number System, i.e., smaller groups of three digits (hundreds, tens, and ones), and convert each group into words.

Consider the below illustration:

Suppose N = 1234567

Now, process this number N in groups of three digits from right to left.

Iteration 1:

  • Current Group: 567
  • Word Representation: Five Hundred Sixty-Seven
  • Multiplier: “”

At this point, the partial word representation is “Five Hundred Sixty-Seven”.

Iteration 2:

  • Current Group: 234
  • Word Representation: Two Hundred Thirty-Four
  • Multiplier: Thousand

The updated word representation becomes “Two Hundred Thirty-Four Thousand Five Hundred Sixty-Seven”.

Iteration 3:

  • Current Group: 1
  • English Representation: One
  • Multiplier: Million

Hence, finally the given number into words becomes “One Million Two Hundred Thirty-Four Thousand Five Hundred Sixty-Seven“.

NOTE: The following code supports numbers up to 15 digits, i.e., numbers from 0 to trillions. The code prints according to the western number system.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
string numberToWords(long long int n)
{
    long long int limit = 1000000000000, curr_hun, t = 0;
   
    // If zero return zero
    if (n == 0)
        return ("Zero");
   
    // Array to store the powers of 10
    string multiplier[] = { "", "Trillion", "Billion",
                            "Million", "Thousand" };
   
    // Array to store numbers till 20
    string first_twenty[] = {
        "",        "One",       "Two",      "Three",
        "Four",    "Five",      "Six",      "Seven",
        "Eight",   "Nine",      "Ten",      "Eleven",
        "Twelve""Thirteen""Fourteen", "Fifteen",
        "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    };
 
    // Array to store multiples of ten
    string tens[]
        = { "",      "Twenty""Thirty", "Forty", "Fifty",
            "Sixty", "Seventy", "Eighty", "Ninety" };
 
    // If number is less than 20, return without any further
    // computation
    if (n < 20)
        return (first_twenty[n]);
 
    // Answer variable to store the conversion
    string answer = "";
    for (long long int i = n; i > 0;
         i %= limit, limit /= 1000) {
 
        // Store the value in multiplier[t], i.e n =
        // 1000000, then r = 1, for multiplier(million), 0
        // for multipliers(trillion and billion)
        curr_hun = i / limit;
 
        // It might be possible that the current multiplier
        // is bigger than your number
        while (curr_hun == 0) {
 
            // Set i as the remainder obtained when n was
            // divided by the limit
            i %= limit;
 
            // Divide the limit by 1000, shifts the
            // multiplier
            limit /= 1000;
 
            // Get the current value in hundreds, as
            // English system works in hundreds
            curr_hun = i / limit;
 
            // Shift the multiplier
            ++t;
        }
 
        // If current hundred is greater than 99, Add the
        // hundreds' place
        if (curr_hun > 99)
            answer += (first_twenty[curr_hun / 100]
                       + " Hundred ");
 
        // Bring the current hundred to tens
        curr_hun = curr_hun % 100;
 
        // If the value in tens belongs to [1,19], add using
        // the first_twenty
 
        if (curr_hun > 0 && curr_hun < 20)
            answer += (first_twenty[curr_hun] + " ");
 
        // If curr_hun is now a multiple of 10, but not 0
        // Add the tens' value using the tens array
        else if (curr_hun % 10 == 0 && curr_hun != 0)
            answer += (tens[curr_hun / 10 - 1] + " ");
 
        // If the value belongs to [21,99], excluding the
        // multiples of 10 Get the ten's place and one's
        // place, and print using the first_twenty array
 
        else if (curr_hun > 20 && curr_hun < 100)
            answer += (tens[curr_hun / 10 - 1] + " "
                       + first_twenty[curr_hun % 10] + " ");
 
        // If Multiplier has not become less than 1000,
        // shift it
        if (t < 4)
            answer += (multiplier[++t] + " ");
    }
    return (answer);
}
 
// Driver code.
int main()
{
    // Input 1
    long long int n = 36;
    cout << numberToWords(n) << '\n';
   
    // Input 2
    n = 123456789;
    cout << numberToWords(n) << '\n';
   
    // Input 3
    n = 10101010110001;
    cout << numberToWords(n) << '\n';
   
    // Input 4
    n = 999999999;
    cout << numberToWords(n) << '\n';
    return 0;
}
// This Code is contributed by Alok Khansali


Java




public class GFG {
    static String numberToWords(long n)
    {
        long limit = 1000000000000L, curr_hun, t = 0;
 
        // If zero return zero
        if (n == 0)
            return ("Zero");
 
        // Array to store the powers of 10
        String multiplier[] = { "", "Trillion", "Billion",
                                "Million", "Thousand" };
 
        // Array to store numbers till 20
        String first_twenty[] = {
            "",        "One",       "Two",      "Three",
            "Four",    "Five",      "Six",      "Seven",
            "Eight",   "Nine",      "Ten",      "Eleven",
            "Twelve""Thirteen""Fourteen", "Fifteen",
            "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
 
        // Array to store multiples of ten
        String tens[] = { "",        "Twenty", "Thirty",
                          "Forty",   "Fifty""Sixty",
                          "Seventy", "Eighty", "Ninety" };
 
        // If number is less than 20, return without any
        // further computation
        if (n < 20L)
            return (first_twenty[(int)n]);
        String answer = "";
        for (long i = n; i > 0; i %= limit, limit /= 1000) {
 
            // Store the value in multiplier[t], i.e n =
            // 1000000, then r = 1, for multiplier(million),
            // 0 for multipliers(trillion and billion)
            // multiplier here refers to the current
            // accessible limit
            curr_hun = i / limit;
 
            // It might be possible that the current
            // multiplier is bigger than your number
            while (curr_hun == 0) {
 
                // Set i as the remainder obtained when n
                // was divided by the limit
                i %= limit;
 
                // Divide the limit by 1000, shifts the
                // multiplier
                limit /= 1000;
 
                // Get the current value in hundreds, as
                // English system works in hundreds
                curr_hun = i / limit;
 
                // Shift the multiplier
                ++t;
            }
 
            // If current hundred is greater than 99, Add
            // the hundreds' place
            if (curr_hun > 99)
                answer += (first_twenty[(int)curr_hun / 100]
                           + " Hundred ");
 
            // Bring the current hundred to tens
            curr_hun = curr_hun % 100;
 
            // If the value in tens belongs to [1,19], add
            // using the first_twenty
            if (curr_hun > 0 && curr_hun < 20)
                answer
                    += (first_twenty[(int)curr_hun] + " ");
 
            // If curr_hun is now a multiple of 10, but not
            // 0 Add the tens' value using the tens array
            else if (curr_hun % 10 == 0 && curr_hun != 0)
                answer
                    += (tens[(int)curr_hun / 10 - 1] + " ");
 
            // If the value belongs to [21,99], excluding
            // the multiples of 10 Get the ten's place and
            // one's place, and print using the first_twenty
            // array
            else if (curr_hun > 20 && curr_hun < 100)
                answer
                    += (tens[(int)curr_hun / 10 - 1] + " "
                        + first_twenty[(int)curr_hun % 10]
                        + " ");
 
            // If Multiplier has not become less than 1000,
            // shift it
            if (t < 4)
                answer += (multiplier[(int)++t] + " ");
        }
        return (answer);
    }
 
    // Driver code
    public static void main(String args[])
    {
        // Input 1
        long n = 36L;
        System.out.println(numberToWords(n));
 
        // Input 2
        n = 123456789;
        System.out.println(numberToWords(n));
 
        // Input 3
        n = 10101010110001L;
        System.out.println(numberToWords(n));
 
        // Input 4
        n = 999999999;
        System.out.println(numberToWords(n));
    }
}
// This Code is contributed by Alok Khansali


Python




def numberToWords(n):
    limit, t = 1000000000000, 0
 
    # If zero print zero
    if (n == 0):
        print("zero")
        return
 
    # Array to store the powers of 10
    multiplier = ["", "Trillion", "Billion", "Million", "Thousand"]
 
    # Array to store numbers till 20
    first_twenty = ["", "One", "Two",
                    "Three", "Four", "Five",
                    "Six", "Seven", "Eight",
                    "Nine", "Ten", "Eleven",
                    "Twelve", "Thirteen", "Fourteen",
                    "Fifteen", "Sixteen", "Seventeen",
                    "Eighteen", "Nineteen"]
 
    # Array to store multiples of ten
    tens = ["", "Twenty", "Thirty", "Forty", "Fifty",
            "Sixty", "Seventy", "Eighty", "Ninety"]
 
    # If number is less than 20, print without any
    if (n < 20):
        print(first_twenty[n])
        return
    answer = ""
    i = n
    while(i > 0):
        '''
        Store the value in multiplier[t], i.e n = 1000000,
        then r = 1, for multiplier(million), 0 for multipliers(trillion and billion)
        multiplier here refers to the current accessible limit
        '''
        curr_hun = i // limit
 
        # It might be possible that the current multiplier is bigger than your number
        while (curr_hun == 0):
 
            # Set i as the remainder obtained when n was divided by the limit
            i %= limit
 
            # Divide the limit by 1000, shifts the multiplier
            limit /= 1000
 
            # Get the current value in hundreds, as English system works in hundreds
            curr_hun = i // limit
 
            # Shift the multiplier
            t += 1
 
        # If current hundred is greater than 99, Add the hundreds' place
        if (curr_hun > 99):
            answer += (first_twenty[curr_hun // 100] + " tensundred ")
 
        # Bring the current hundred to tens
        curr_hun = curr_hun % 100
 
        # If the value in tens belongs to [1,19], add using the first_twenty
        if (curr_hun > 0 and curr_hun < 20):
            answer += (first_twenty[curr_hun] + " ")
 
        # If curr_hun is now a multiple of 10, but not 0
        # Add the tens' value using the tens array
        elif (curr_hun % 10 == 0 and curr_hun != 0):
            answer += (tens[(curr_hun//10) - 1] + " ")
 
        # If the value belongs to [21,99], excluding the multiples of 10
        # Get the ten's place and one's place, and print using the first_twenty array
        elif (curr_hun > 19 and curr_hun < 100):
            answer += (tens[(curr_hun//10) - 1] + " " +
                       first_twenty[curr_hun % 10] + " ")
 
        # If Multiplier has not become less than 1000, shift it
        if (t < 4):
            answer += (multiplier[t] + " ")
 
        i = i % limit
        limit = limit // 1000
 
    print(answer)
 
 
# Input 1
n = 36
numberToWords(n)
 
# Input 2
n = 123456789
numberToWords(n)
 
# Input 3
n = 10101010110001
numberToWords(n)
 
# Input 4
n = 999999999
numberToWords(n)
 
# This code is contributed by Alok Khansali


C#




// Include namespace system
using System;
 
public class numtowords {
    public static String numberToWords(long n)
    {
        var limit = 1000000000000L;
        long curr_hun;
        var t = 0;
       
        // If zero return zero
        if (n == 0) {
            return ("Zero");
        }
        // Array to store the powers of 10
        String[] multiplier = { "", "Trillion", "Billion",
                                "Million", "Thousand" };
        // Array to store numbers till 20
        String[] first_twenty = {
            "",        "One",       "Two",      "Three",
            "Four",    "Five",      "Six",      "Seven",
            "Eight",   "Nine",      "Ten",      "Eleven",
            "Twelve""Thirteen""Fourteen", "Fifteen",
            "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
       
        // Array to store multiples of ten
        String[] tens = { "",        "Twenty", "Thirty",
                          "Forty",   "Fifty""Sixty",
                          "Seventy", "Eighty", "Ninety" };
       
        // If number is less than 20, return without any
        // further computation
        if (n < 20L) {
            return (first_twenty[(int)n]);
        }
        var answer = "";
        for (long i = n; i > 0; i %= limit, limit /= 1000) {
           
            // Store the value in multiplier[t], i.e n =
            // 1000000, then r = 1, for multiplier(million),
            // 0 for multipliers(trillion and billion)
            // multiplier here refers to the current
            // accessible limit
           
            curr_hun = i / limit;
           
            // It might be possible that the current
            // multiplier is bigger than your number
            while (curr_hun == 0) {
               
                // Set i as the remainder obtained when n
                // was divided by the limit
                i %= limit;
               
                // Divide the limit by 1000, shifts the
                // multiplier
                limit /= 1000;
               
                // Get the current value in hundreds, as
                // English system works in hundreds
               
                curr_hun = i / limit;
               
                // Shift the multiplier
                ++t;
            }
           
            // If current hundred is greater than 99, Add
            // the hundreds' place
            if (curr_hun > 99) {
                answer += (first_twenty[(int)((int)curr_hun
                                              / 100)]
                           + " Hundred ");
            }
           
            // Bring the current hundred to tens
            curr_hun = curr_hun % 100;
           
            // If the value in tens belongs to [1,19], add
            // using the first_twenty
           
            if (curr_hun > 0 && curr_hun < 20) {
                answer
                    += (first_twenty[(int)curr_hun] + " ");
            }
            else if (curr_hun % 10 == 0 && curr_hun != 0) {
                answer
                    += (tens[(int)((int)curr_hun / 10) - 1]
                        + " ");
            }
            else if (curr_hun > 20 && curr_hun < 100) {
                answer
                    += (tens[(int)((int)curr_hun / 10) - 1]
                        + " "
                        + first_twenty[(int)curr_hun % 10]
                        + " ");
            }
           
            // If Multiplier has not become less than 1000,
            // shift it
            if (t < 4) {
                answer += (multiplier[(int)++t] + " ");
            }
        }
        return (answer);
    }
   
    // Driver code
    public static void Main(String[] args)
    {
        // Input 1
        var n = 36L;
        Console.WriteLine(numtowords.numberToWords(n));
       
        // Input 2
        n = 123456789;
        Console.WriteLine(numtowords.numberToWords(n));
       
        // Input 3
        n = 10101010110001L;
        Console.WriteLine(numtowords.numberToWords(n));
       
        // Input 4
        n = 999999999;
        Console.WriteLine(numtowords.numberToWords(n));
    }
}


Javascript




function numberToWords(n)
{
    let limit = 1000000000000, t = 0
    // If zero console.log zero
    if (n == 0)
    {
        console.log("zero")
        return
    }
    // Array to store the powers of 10
    let multiplier = ["", "Trillion", "Billion", "Million", "Thousand"]
 
    // Array to store numbers till 20
    let first_twenty = ["", "One", "Two",
                    "Three", "Four", "Five",
                    "Six", "Seven", "Eight",
                    "Nine", "Ten", "Eleven",
                    "Twelve", "Thirteen", "Fourteen",
                    "Fifteen", "Sixteen", "Seventeen",
                    "Eighteen", "Nineteen"]
 
    // Array to store multiples of ten
    let tens = ["", "Twenty", "Thirty", "Forty", "Fifty",
            "Sixty", "Seventy", "Eighty", "Ninety"]
 
    // If number is less than 20, console.log without any
    if (n < 20)
    {
        console.log(first_twenty[n])
        return
    }
    let answer = ""
    let i = n
    while(i > 0)
    {
        /*
        Store the value in multiplier[t], i.e n = 1000000,
        then r = 1, for multiplier(million), 0 for multipliers(trillion and billion)
        multiplier here refers to the current accessible limit
        */
        let curr_hun = Math.floor(i / limit)
 
        // It might be possible that the current multiplier is bigger than your number
        while (curr_hun == 0)
        {
            // Set i as the remainder obtained when n was divided by the limit
            i %= limit
 
            // Divide the limit by 1000, shifts the multiplier
            limit /= 1000
 
            // Get the current value in hundreds, as English system works in hundreds
            curr_hun = Math.floor(i / limit)
 
            // Shift the multiplier
            t += 1
        }
 
        let flr = Math.floor(curr_hun / 100);
 
        // If current hundred is greater than 99, Add the hundreds' place
        if (curr_hun > 99)
            answer += (first_twenty[flr] + " tensundred ")
 
        // Bring the current hundred to tens
        curr_hun = curr_hun % 100
 
        // If the value in tens belongs to [1,19], add using the first_twenty
        if (curr_hun > 0 && curr_hun < 20)
            answer += (first_twenty[curr_hun] + " ")
 
        // If curr_hun is now a multiple of 10, but not 0
        // Add the tens' value using the tens array
        else if (curr_hun % 10 == 0  &&  curr_hun != 0){
            flr = Math.floor(curr_hun / 10);
            answer += (tens[flr - 1] + " ")
        }
 
        // If the value belongs to [21,99], excluding the multiples of 10
        // Get the ten's place and one's place, and console.log using the first_twenty array
        else if (curr_hun > 19  &&  curr_hun < 100){
            flr = Math.floor(curr_hun / 10);
            answer += (tens[flr - 1] + " " +
                       first_twenty[curr_hun % 10] + " ")
        }
 
        // If Multiplier has not become less than 1000, shift it
        if (t < 4)
            answer += (multiplier[t] + " ")
             
        i = i % limit
        limit = Math.floor(limit / 1000)
    }
 
    console.log(answer)
}
 
// Input 1
let n = 36
 
numberToWords(n)
n = 123456789
 
// Input 2
numberToWords(n)
n = 10101010110001
 
// Input 3
numberToWords(n)
 
// Input 4
n = 999999999
numberToWords(n)
 
// This code is contributed by phasing17.


Output

Thirty Six 
One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Eighty Nine 
Ten Trillion One Hundred One Billion Ten Million One Hundred Ten Thousand One 
Nine Hundred Nine...

Time Complexity: O(Log10(N))
Auxiliary Space: O(1)



Similar Reads

Program to convert a given number to words | Set 2
Write code to convert a given number into words. Examples: Input: 438237764Output: forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four Input: 999999Output: nine lakh ninety nine thousand nine hundred and ninety nine Input: 1000Output: one thousand Explanation: 1000 in words is "one thousand" Recommended PracticeInte
13 min read
Check if the given string of words can be formed from words present in the dictionary
Given a string array of M words and a dictionary of N words. The task is to check if the given string of words can be formed from words present in the dictionary. Examples: dict[] = { find, a, geeks, all, for, on, geeks, answers, inter } Input: str[] = { "find", "all", "answers", "on", "geeks", "for", "geeks" }; Output: YES all words of str[] are p
17 min read
Find all words from String present after given N words
Given a string S and a list lis[] of N number of words, the task is to find every possible (N+1)th word from string S such that the 'second' word comes immediately after the 'first' word, the 'third' word comes immediately after the 'second' word, the 'fourth' word comes immediately after the 'third' word and so on. Examples: Input: S = “Siddhesh i
11 min read
Count words that appear exactly two times in an array of words
Given an array of n words. Some words are repeated twice, we need to count such words. Examples: Input : s[] = {"hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace"}; Output : 1 There is only one word "hate" that appears twice Input : s[] = {"Om", "Om", "Shankar", "Tripathi", "Tom", "Jerry", "Jerry"}; Output : 2 There
6 min read
Convert given Array of Integers into Words
Given an array arr[] of N elements which are in range [0, 9]. the task is to convert each array element into its numeric strings. Examples: Input: arr[] = [1, 4, 3, 2, 6]Output: one four three two six Input: arr[]= [0, 4, 4, 6, 9]Output: zero four four six nine Approach: The problem can be solved with the help of map. Follow the steps given below:
4 min read
Convert given time into words
Given a time in the format of hh:mm (12-hour format) 0 &lt; hh &lt; 12, 0 &lt;= mm &lt; 60. The task is to convert it into words as shown:Examples : Input : h = 5, m = 0 Output : five o' clock Input : h = 6, m = 24 Output : twenty four minutes past six Corner cases are m = 0, m = 15, m = 30 and m = 45. 6:00 six o'clock 6:10 ten minutes past six 6:1
7 min read
Convert numeric words to numbers
Given a string S, containing numeric words, the task is to convert the given string to the actual number. Example: Input: S = "zero four zero one"Output: 0401 Input: S = "four zero one four"Output: 4014 Method #1: Using loop + join() + split() One of the ways to solve this problem is to use a map, where one can map the numeric with words and then s
6 min read
Program to print the given digit in words
Given a number N, the task is to convert every digit of the number into words. Examples: Input: N = 1234 Output: One Two Three Four Explanation: Every digit of the given number has been converted into its corresponding word. Input: N = 567 Output: Five Six Seven Approach: The idea is to traverse through every digit of the number and use switch-case
12 min read
Program to reverse words in a given string in C++
Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++. Examples: Input: str = "the sky is blue" Output: blue is sky theInput: str = "I love programming" Output: programming love I Method 1: Using STL functions Reverse the given string str using STL function reverse().Iterate the reversed string an
6 min read
C++ Program To Reverse Words In A Given String
Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "practice of lot a needs coding at good getting" Rec
7 min read