### Number to Word

I was reading my C++ book and wanted to practice what i was reading. I encountered a programming challenge which was to make a program that converts numbers from
0-9999 to words.

ex. 4578-> four thousand five hundred and seventy eight

i already did my code (as you can see) but i only put up this topic for people to discuss more efficient ways or simply different ways of doing this program.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` `````` #include #include using namespace::std; class Numbers { private: int num; public: static string twoDigit[]; Numbers() { num=0;} void setNumber(int number) { num=number;} int getNumber() { return num;} void print() { if(num>=1000) { int thousand; thousand=(num%10000-num%1000)/1000; cout<=100) { int hundred; hundred=(num%1000-num%100)/100; cout<=0) { int ten; ten=num%100; cout<>number; if(number<0||number>9999) { cout<<"Error.Number is out of range."<>number; } conversion.setNumber(number); conversion.print(); system("pause"); return 0; }``````
 but i only put up this topic for people to discuss more efficient ways or simply different ways of doing this program

switch out the if condition in main for a while loop. If you enter an incorrect number twice, the program ends. get rid of system pause
thanks for noticing...i originally thought of doing it a while..guess i had something else on my mind haha
closed account (S6k9GNh0)
You can actually do this methodically... especially if you're dealing with the decimal system (probably with other number systems as well but I don't know how).

Let's say you want to do be able to convert up to 5 digits to words. You can divide the fifth, fourth, third, second, and first digit to determine which number it actually is.

(53424) / 10000 = 5
(53424 - (50000)) / 1000 = 3
(53424 - (50000 + 3000)) / 100 = 4
(53424 - (50000 + 3000 + 400)) / 10 = 2
(53424 - (50000 + 3000 + 400 + 20)) = 4

Given that you know each number (supposing you figure out the pattern above), you can translate that to English numerics which also has a pattern.
Last edited on
This is a bit off topic, but I made this a while ago and all it does is to write back the number you typed not the words though. Just thought I should share with you.

http://ideone.com/bdlRgH
Topic archived. No new replies allowed.