Write code to convert a given number into words. For example, if “1234” is given as input, output should be “one thousand two hundred thirty four”.
Following is the implementation for the same. The code supports numbers up-to 4 digits, i.e., numbers from 0 to 9999. Idea is to create arrays that store individual parts of output strings. One array is used for single digits, one for numbers from 10 to 19, one for 20, 30, 40, 50, .. etc, and one for powers of 10.
The given number is divided in two parts: first two digits and last two digits, and the two parts are printed separately.
9923: nine thousand nine hundred twenty three 523: five hundred twenty three 89: eighty nine 8989: eight thousand nine hundred eighty nine
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to convert a given number to words | Set 2
- Count words that appear exactly two times in an array of words
- Program to print the given digit in words
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Program to Convert Octal Number to Binary Number
- Program to Convert BCD number into Decimal number
- Given a sequence of words, print all anagrams together | Set 2
- Given a sequence of words, print all anagrams together | Set 1
- Reverse words in a given string
- Count words in a given string
- Given a sequence of words, print all anagrams together using STL
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Sort an Array alphabetically when each number is converted into words
- Convert a number m to n using minimum number of given operations
- Smallest number to be subtracted to convert given number to a palindrome
- Program to convert Number in characters
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Program to convert a BCD to Hexa-Decimal Number
- Program to Convert Hexadecimal Number to Binary
- Boggle (Find all possible words in a board of characters) | Set 1