Answer to Question #47566 in C++ for krad

Question #47566
Write a program that accepts a number and outputs its equivalent in words:
Sample input/output dialogue:
Enter number: 1380
One thousand three hundred eighty
1
Expert's answer
2014-10-13T03:52:50-0400
case 9: word += "ninety "; break;
}
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
1380
one thousand three hundred eighty



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