Answer to Question #46314 in C++ for Enrique

Question #46314
Write a program that reads a
whole number of up to nine digits and prints it in words.
For example, the input 13247 ought to produce "thirteen
thousand two hundred forty seven"
1
Expert's answer
2014-10-02T02:44:33-0400
}
switch (unit) {
case 1: word += "one "; break;
case 2: word += "two "; break;
case 3: word += "three "; break;
case 4: word += "four "; break;
case 5: word += "five "; break;
case 6: word += "six "; break;
case 7: word += "seven "; break;
case 8: word += "eight "; break;
case 9: word += "nine "; break;
}
}

return word;
}

// Converts to three digits number to word
string threeDigits(long number) {
stringword = "";
long residue = number % 100;
long hundred = number / 100;
switch (hundred) {
case 1: word += "one hundred "; break;
case 2: word += "two hundred "; break;
case 3: word += "three hundred"; break;
case 4: word += "four hundred "; break;
case 5: word += "five hundred "; break;
case 6: word += "six hundred "; break;
case 7: word += "seven hundred"; break;
case 8: word += "eight hundred"; break;
case 9: word += "nine hundred "; break;
}
word += twoDigits(residue);

return word;
}

// Converts nine digits number to word
string nineDigits(long number) {
stringword = "";
long hundred = number % 1000;
long thousand = (number % 1000000) / 1000;
long million = number / 1000000;

if (million != 0) {
word += threeDigits(million) + "million ";
}

if (thousand != 0) {
word += threeDigits(thousand) + "thousand ";
}

if (hundred != 0) {
word += threeDigits(hundred);
}

return word;
}

int main() {
// Input/Output
long number;
cin >> number;
cout << nineDigits(number);
return 0;
}
Result
13147
thirteen thousand one hundred forty seven


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS