68 774
Assignments Done
98,8%
Successfully Done
In January 2019

# Answer to Question #61603 in C++ for Daniel Greef

Question #61603
Hi, could you make me a program that when I input any number from 0-1,000,000 it will print the word value using if and else statements or switch/case statements. arrays and strings would be possible too.
e.g. input 4, output four. Comment lines would also be appreciated, thanks!
Condition: Hi, could you make me a program that when I input any number from 0-1,000,000 it will print the word value using if and else statements or switch/case statements. arrays and strings would be possible too. e.g. input 4, output four. Comment lines would also be appreciated, thanks!
Solution (In C++):
#include <iostream>
#include <string>

using namespace std;

//An array of the first 14 numbers and zero
const string first14 [15] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen" };
//An array of prefixes
const string prefixes [8] = { "twen", "thir", "for", "fif", "six", "seven", "eigh", "nine" };

//Converter function
string IntToStr(const unsigned int number)
{
if (number <= 14)
return first14[number];
if (number < 20)
return prefixes[number-12] + "teen";
if (number < 100) {
unsigned int remainder = number - ((int)(number / 10) * 10);
return prefixes[number / 10 - 2] + (0 != remainder ? "ty " + IntToStr(remainder) : "ty");
/* The same code as:
if (0!=remainder) { return prefixes[number / 10 - 2] + "ty " + IntToStr(remainder); }
else { return prefixes[number / 10 - 2]+"ty"; }
but i use a short version of the operator (if/else), Which looks like: (Condition) ? true : false;
*/

}
if (number < 1000) {
unsigned int remainder = number - (int)((number / 100) * 100);
return first14[number / 100] + (0 != remainder ? " hundred " + IntToStr(remainder) : " hundred");
}
if (number < 1000000) {
unsigned int thousands = (int)(number / 1000);
unsigned int remainder = number - (thousands * 1000);
return IntToStr(thousands) + (0 != remainder ? " thousand " + IntToStr(remainder) : " thousand");
}
if (number == 1000000) return "one million";
if (number > 1000000) return "Out of range";
}

int main()
{
int num;
cout << "Enter the number from 0-1000000: ";
cin >> num;
cout << "Word value: " << IntToStr(num) << std::endl;
cin.get();
cin.get();
return 0;
}

Examples of the work program

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!