hello guys i got a simple project but not that simple at the same time. lol The user needs to input a number such as : 512 and the code needs to output the number but in letters(five hundread and twelve). Any tips? :)
Let's look at a simple example of how you might do this. Imagine you were give the number 72061.
The first thing you should do is determine how many digits there are - in this case 5. Now, you can divide by 10^4 (10 to the power 4, 4 because it is one less than the number of digits) and you will get 7 as the answer. Now you know that 7 is the first digit, and because you used 4 as the power, you know that this is the ten thousands digit. So you can store "Seventy". Now subtract off 10^4 * 7 and you will have the leftover amount of 2061.
Now you simply repeat the process after decrementing the power. Divide by 10^3 and you get 2. Since we used the power 3, we know this is the thousands digit, so we can store "Two Thousand". Then, subtract off 10^3 * 2 leaving 61.
Repeat again, divide by 10^2 to find the hundreds digit. Here the answer will be 0, so we don't need to store anything. Note that in some cases you will still need to (if for example the thousands digit was 0 but the ten thousands digit was 7, you need to still store "Thousand").
Repeat again, divide by 10^1 and get the digit 6 which is the tens digit. Store "Sixty" and subtract 10^1 * 6 to get 1.
Repeat again, dividing by 10^0 (if u really want to, although this is just dividing by 1) and you will get the units digit 1. Store "One".
So you stored: "Seventy Two Thousand Sixty One".
I'm sure there are other ways to approach the problem, but this one will probably work well.
jhonnyboy, it will be better if you can try to write a program first then post your coding.
mahlerfive is right but another alternative way is to store the number the user input in string then you can get each digit in the string a lot more easier. After you get the single digit, use "switch" for each digit to display the number in word. :)
using namespace std;
case 1: ones(number);
void ones(string numero)
case 1: cout<<"one"<<endl;
case 2: cout<<"two"<<endl;
case 3: cout<<"three"<<endl;
case 4: cout<<"four"<<endl;
case 5: cout<<"five"<<endl;
case 6: cout<<"six"<<endl;
case 7: cout<<"seven"<<endl;
case 8: cout<<"eight"<<endl;
case 9: cout<<"nine"<<endl;
case 0: cout<<"zero"<<endl;
It's giving me a hard time when i try to convert the string into an integer for the converting. Don't know what to do :(