Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to express a positive integer number in words in C++
Suppose we are given a positive integer number. We have to spell the number in words; like if a number "56" is given as input the output will be "Fifty-Six". The range of conversion is up to a billion.
So, if the input is like input = 5678, then the output will be Five Thousand Six Hundred Seventy-Eight.
To solve this, we will follow these steps −
- Define an array ‘numbers’ that contain pairs such as − {{"Billion", 1000000000},
- {"Million", 1000000},
- {"Thousand", 1000},
- {"Hundred", 100},
- {"Ninety", 90},
- {"Eighty", 80},
- {"Seventy", 70},
- {"Sixty", 60},
- {"Fifty", 50},
- {"Forty", 40},
- {"Thirty", 30},
- {"Twenty", 20},
- {"Nineteen", 19},
- {"Eighteen", 18},
- {"Seventeen", 17},
- {"Sixteen", 16},
- {"Fifteen", 15},
- {"Fourteen", 14},
- {"Thirteen", 13},
- {"Twelve", 12},
- {"Eleven", 11},
- {"Ten", 10},
- {"Nine", 9},
- {"Eight", 8},
- {"Seven", 7},
- {"Six", 6},
- {"Five", 5},
- {"Four", 4},
- {"Three", 3},
- {"Two", 2},
- {"One", 1}}
- Define a function solve(). This takes input.
- if input is same as 0, then −
- return "Zero"
- for each num in array numbers, do
- if second value of num <= input, then −
- if second value of num >= 100, then −
- result := solve(input / second value of num)
- if input > (input / second value of num) * second value of m, then −
- result := result + " " + solve(input - (input / second value of num))
- otherwise,
- result := first value of num + ((if input > second value of num , then: " " + solve(input - second value of num), otherwise: " "))
- Come out from the loop
- if second value of num >= 100, then −
- if second value of num <= input, then −
- return result
- if input is same as 0, then −
- solve(input)
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h>
using namespace std;
vector<pair<string, int>> numbers{{"Billion", 1000000000},
{"Million", 1000000},
{"Thousand", 1000},
{"Hundred", 100},
{"Ninety", 90},
{"Eighty", 80},
{"Seventy", 70},
{"Sixty", 60},
{"Fifty", 50},
{"Forty", 40},
{"Thirty", 30},
{"Twenty", 20},
{"Nineteen", 19},
{"Eighteen", 18},
{"Seventeen", 17},
{"Sixteen", 16},
{"Fifteen", 15},
{"Fourteen", 14},
{"Thirteen", 13},
{"Twelve", 12},
{"Eleven", 11},
{"Ten", 10},
{"Nine", 9},
{"Eight", 8},
{"Seven", 7},
{"Six", 6},
{"Five", 5},
{"Four", 4},
{"Three", 3},
{"Two", 2},
{"One", 1}};
string solve(int input) {
if (input == 0) return "Zero";
string result;
for (auto& num : numbers) {
if (num.second <= input) {
if (num.second >= 100) {
result = solve(input / num.second) + " " + num.first;
if (input > (input / num.second) * num.second)
result += " " + solve(input - (input / num.second) * num.second);
} else {
result = num.first + (input > num.second ? " " + solve(input - num.second) : "");
}
break;
}
}
return result;
}
int main() {
cout<< solve(5678) <<endl;
return 0;
}
Input
5678
Output
Five Thousand Six Hundred Seventy Eight
Advertisements