Created
January 26, 2017 12:04
-
-
Save bhrigu123/7e4e140a6452a7823f1a81bb96c33ec8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <map> | |
using namespace std; | |
void makeMapping(map<int, string>& mapping) { | |
mapping[0] = ""; | |
mapping[1] = "One"; | |
mapping[2] = "Two"; | |
mapping[3] = "Three"; | |
mapping[4] = "Four"; | |
mapping[5] = "Five"; | |
mapping[6] = "Six"; | |
mapping[7] = "Seven"; | |
mapping[8] = "Eight"; | |
mapping[9] = "Nine"; | |
mapping[10] = "Ten"; | |
mapping[11] = "Eleven"; | |
mapping[12] = "Twelve"; | |
mapping[13] = "Thirteen"; | |
mapping[14] = "Fourteen"; | |
mapping[15] = "Fifteen"; | |
mapping[16] = "Sixteen"; | |
mapping[17] = "Seventeen"; | |
mapping[18] = "Eighteen"; | |
mapping[19] = "Nineteen"; | |
mapping[20] = "Twenty"; | |
mapping[30] = "Thirty"; | |
mapping[40] = "Forty"; | |
mapping[50] = "Fifty"; | |
mapping[60] = "Sixty"; | |
mapping[70] = "Seventy"; | |
mapping[80] = "Eighty"; | |
mapping[90] = "Ninety"; | |
} | |
string getTensWords(int num, map<int, string>& mapping) { | |
if(num<=20) { | |
return mapping[num]; | |
} | |
int tens = (num/10) * 10; | |
int ones = num%10; | |
string ans = mapping[tens]; | |
if(ones > 0) { | |
ans = ans + " " + mapping[ones]; | |
} | |
return ans; | |
} | |
string getHunderedsWords(int num, map<int, string>& mapping) { | |
//cout << num << ": "; | |
int rem = num%100; | |
num = num/100; | |
string output = ""; | |
if(num > 0) { | |
output = mapping[num]; | |
output = output + " " + "Hundered"; | |
if(rem > 0) { | |
output += " "; | |
} | |
//cout << output << ", "; | |
} | |
if(rem>0) { | |
string t = getTensWords(rem, mapping); | |
//cout << t << endl; | |
output = output + t; | |
} | |
return output; | |
} | |
string getBaseString(int base, int num, int rem) { | |
if(base==0 || num==0) { | |
return ""; | |
} | |
switch(base) { | |
case 1: return "Thousand"; | |
case 2: return "Million"; | |
case 3: return "Billion"; | |
} | |
} | |
string numberToWords(int num) { | |
if(num == 0) { | |
return "Zero"; | |
} | |
map<int, string> mapping; | |
makeMapping(mapping); | |
int base = 1; | |
string output = ""; | |
while(num>0) { | |
int rem = num%1000; | |
num = num/1000; | |
string h = getHunderedsWords(rem, mapping); | |
string baseString = getBaseString(base, num, rem); | |
//cout << h << endl; | |
if(h.length()>0) { | |
if(output.length() == 0) { | |
output = h; | |
} | |
else { | |
output = h + " " + output; | |
} | |
} | |
if(baseString.length()>0 && num%1000 > 0) { | |
if(output.length()>0) { | |
output = " " + output; | |
} | |
output = baseString + output; | |
} | |
base++; | |
} | |
return output; | |
} | |
int main() { | |
int n = 1000000; | |
string output = numberToWords(n); | |
cout << n << endl; | |
cout << output << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment