Write code to convert a given number into words.
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.
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 email@example.com. 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.
- Write a program to calculate pow(x,n)
- Write a program to reverse digits of a number
- Program to find whether a no is power of two
- Write an Efficient Method to Check if a Number is Multiple of 3
- Program to find parity
- Write an Efficient C Program to Reverse Bits of a Number
- Write a program to print all permutations of a given string
- Write a program to add two numbers in base 14
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Program for Fibonacci numbers
- Check if a number is multiple of 5 without using / and % operators
- Given a number, find the next smallest palindrome