Works for any number from 0 to 999999999.
This program gets a number from the user, divides it into three parts and stores them separately in an array. The three numbers are passed through a function that convert them into words. Then it adds "million" to the first part and "thousand" to the second part.
#include <iostream>
using namespace std;
int buffer = 0, partFunc[3] = {0, 0, 0}, part[3] = {0, 0, 0}, a, b, c, d;
long input, nFake = 0;
const char ones[][20] = {"", "one", "two", "three",
"four", "five", "six", "seven",
"eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};
const char tens[][20] = {"", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"};
void convert(int funcVar);
int main() {
cout << "Enter the number:";
cin >> input;
nFake = input;
buffer = 0;
while (nFake) {
part[buffer] = nFake % 1000;
nFake /= 1000;
buffer++;
}
if (buffer == 0) {
cout << "Zero.";
} else if (buffer == 1) {
convert(part[0]);
} else if (buffer == 2) {
convert(part[1]);
cout << " thousand,";
convert(part[0]);
} else {
convert(part[2]);
cout << " million,";
if (part[1]) {
convert(part[1]);
cout << " thousand,";
} else {
cout << "";
}
convert(part[0]);
}
system("pause");
return (0);
}
void convert(int funcVar) {
buffer = 0;
if (funcVar >= 100) {
a = funcVar / 100;
b = funcVar % 100;
if (b)
cout << " " << ones[a] << " hundred and";
else
cout << " " << ones[a] << " hundred ";
if (b < 20)
cout << " " << ones[b];
else {
c = b / 10;
cout << " " << tens[c];
d = b % 10;
cout << " " << ones[d];
}
} else {
b = funcVar;
if (b < 20)
cout << ones[b];
else {
c = b / 10;
cout << tens[c];
d = b % 10;
cout << " " << ones[d];
}
}
}