VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-convert-a-given-number-to-words-set-2/

⇱ Program to convert a given number to words | Set 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to convert a given number to words | Set 2

Last Updated : 28 Feb, 2024

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

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

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.


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.
Comment
Article Tags:
Article Tags: