Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Program to convert a given number to words | Set 2
Next article icon

Program to convert a given number to words | Set 2

Last Updated : 28 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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
Explanation: 1000 in words is "one thousand"

Recommended Practice
Integer to Words
Try It!

We have already discussed an approach that handles numbers from 0 to 9999 in the previous post.

Solution: This approach can handle 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 digits 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 the above place value chart and handle them starting from the Most Significant Digit.

Here's a simple implementation that supports numbers having a maximum of 9 digits. The program can be easily extended to support any 20-digit number.

C++
/* 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 are 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), "");        //Handling the n=0 case     if(out=="")     out = "zero";      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; } 
Java
/* 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 are 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));     } } 
Python3
# 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 are 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 
C#
/* 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 are 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 
JavaScript
<script>  /* Javascript 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     var 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 are to     // make array indexing simple     var ten = [ "", "", "twenty ", "thirty ", "forty ",               "fifty ", "sixty ", "seventy ", "eighty ",                           "ninety " ];      // n is 1- or 2-digit number     function numToWords(n, s)     {         var str = "";         // if n is more than 19, divide it         if (n > 19) {             str += ten[parseInt(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     function convertToWords(n)     {         // stores word representation of given number n         var out = "";          // handles digits at ten millions and hundred         // millions places (if any)         out += numToWords(parseInt(n / 10000000),          "crore ");          // handles digits at hundred thousands and one         // millions places (if any)         out += numToWords(parseInt((n / 100000) % 100),         "lakh ");          // handles digits at thousands and tens thousands         // places (if any)         out += numToWords(parseInt((n / 1000) % 100),          "thousand ");          // handles digit at hundreds places (if any)         out += numToWords(parseInt((n / 100) % 10),          "hundred ");          if (n > 100 && n % 100 > 0) {             out += "and ";         }          // handles digits at ones and tens places (if any)         out += numToWords(parseInt(n % 100), "");          return out;     }      // Driver code  // var handles upto 9 digit no     // change to unsigned var var var to     // handle more digit number     var n = 438237764;      // convert given number in words     document.write(convertToWords(n));      // This code is contributed by Amit Katiyar   </script> 
PHP
<?php /* PHP 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 = array("", "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 are to make array indexing simple $ten = array("", "", "twenty ", "thirty ", "forty ",              "fifty ", "sixty ", "seventy ", "eighty ",              "ninety ");  // n is 1- or 2-digit number function numToWords($n, $s) {     global $one, $ten;     $str = "";          // if n is more than 19, divide it     if ($n > 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  

Complexity Analysis: 

  • Time complexity: O(1). 
    The loop runs for a constant amount of time.
  • Auxiliary space: O(1). 
    As no extra space is required.
     

Next Article
Program to convert a given number to words | Set 2

A

aditya.goel
Improve
Article Tags :
  • Mathematical
  • DSA
Practice Tags :
  • Mathematical

Similar Reads

    Program to convert a given number to words
    Given a non-negative integer n, the task is to convert the given number into its English representation according to International Number System.Examples:Input: n = 0Output: "Zero"Input: n = 123Output: "One Hundred Twenty Three"Input: n = 10245Output: "Ten Thousand Two Hundred Forty Five"Input: n =
    15 min read
    Lex Program to count number of words
    Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator) Examp
    1 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 wi
    4 min read
    Convert numeric words to numbers
    The goal is to convert numeric words (such as "zero", "one", "two", etc.) into their corresponding digit forms (e.g., "0", "1", "2") to facilitate numerical operations. For example, in the string "zero four zero one", we aim to convert it into the string "0401". Let's explore different approaches to
    3 min read
    Count words in a given string
    Given a string, count the number of words in it. The words are separated by the following characters: space (' ') or new line ('\n') or tab ('\t') or a combination of these. Recommended PracticeCount number of wordsTry It!Method 1: The idea is to maintain two states: IN and OUT. The state OUT indica
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences