
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Number to word conversion
This algorithm will convert a given number into English words. Like 564 will be Five Hundred and Sixty-Four.
For this algorithm, some predefined strings are given, from that list, it will get the proper words to make into words.
The lists are like
- Units: it will hold all words for (0 to 9) like Zero, One…Nine
- twoDigits: it will hold all numbers from (10 - 19), like Ten, eleven…Nineteen
- tenMul: For ten multiples, (20-90), like Twenty, Thirty, … Ninety.
- tenPower: It is for Hundred and Thousands as power 2 and 3 of 10
Input and Output
Input: The number: 568 Output: Five Hundred And Sixty Eight
Algorithm
numToWord(num)
there are some lists which hold the words for different integers
Input: The number.
Output: Represent number into words.
Begin if n >= 0 and n < 10, then display units(n) into words else if n >= 10 and n < 20, then display twoDigitNum(n) into words //It is from ten to nineteen else if n >= 20 and n <100, then display tensMultiple(n/10), into words if n mod 10 ≠ 0, then numToWord(n mod 10) else if n >= 100 and n < 1000, then display units(n/100), into words display “Hundred”, into words //Hundred if n mod 100 ≠ 0, then display “And” numToWord(n mod 100) else if n >= 1000 and n <= 32767, then numToWord(n/1000) display “Thousand” if n mod 1000 ≠ 0, then numToWord(n mod 1000) else display invalid number and exit End
Example
#include<iostream> using namespace std; string getUnit(int n) { //Return single digit to word string unit[10] = {"Zero", "One","Two", "Three","Four","Five", "Six","Seven","Eight","Nine"}; return unit[n]; } string getTwoDigits(int n) { //Here n is 2 digit number string td[10] = {"Ten", "Eleven","Twelve","Thirteen", "Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; return td[n%10]; } string getTenMul(int n) { //Here n is multiple of 10 string tm[8] = {"Twenty", "Thirty","Fourty", "Fifty","Sixty", "Seventy","Eighty","Ninty"}; return tm[n-2]; } string getTenPow(int pow) { //The power of ten in words string power[2] = {"Hundred", "Thousand"}; return power[pow-2]; } void printNumToWord(int n) { if(n >= 0 && n < 10) cout << getUnit(n) << " "; //Unit values to word else if(n >= 10 && n < 20) cout << getTwoDigits(n) << " "; //from eleven to nineteen else if(n >= 20 && n < 100) { cout << getTenMul(n/10)<<" "; if(n%10 != 0) printNumToWord(n%10); //Recursive call to convert num to word }else if(n >= 100 && n < 1000) { cout << getUnit(n/100)<<" "; cout <<getTenPow(2) << " "; if(n%100 != 0) { cout << "And "; printNumToWord(n%100); } }else if(n >= 1000 && n <= 32767) { printNumToWord(n/1000); cout <<getTenPow(3)<<" "; if(n%1000 != 0) printNumToWord(n%1000); }else printf("Invalid Input"); } main() { int number; cout << "Enter a number between 0 to 32767: "; cin >> number; printNumToWord(number); }
Output
Enter a number between 0 to 32767: 568 Five Hundred And Sixty Eight
- Related Articles
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
- Add number strings without using conversion library methods in JavaScript
- Decimal to Binary conversion\n
- Explain Digital to Analog Conversion
- Print all substring of a number without any conversion in C++
- C++ program to read file word by word?
- Conversion Disorder
- Energy Conversion
- Program to find number of steps required to change one word to another in Python
- Binary to BCD conversion in 8051
- BCD to binary conversion in 8051
- Hex to ASCII conversion in 8051
- Bool to int conversion in C++
- Conversion of Java Maps to List
- Prefix to Postfix Conversion in C++

Advertisements