View Full Version : logic for converting a number into words


abdulla
08-24-03, 12:18 AM
Hi Everybody!!!

I recently faced an Interview in one of the reputed MNCs in the world as was a fresher I was asked more questions on logic. Though I copuld resolve all the problems I could not effectively solve one problem and that was to convert a number into words for example, 123 is One hundred and twenty three. I just want to know whether there is any better logic to solve this problem apart from using only swich cases.

I hope I get the response as earlier
Thanx one and all
Bye

okinrus
08-24-03, 04:48 PM
A simple fast method that wasts alot of memory would be to use a look up table. Another way that does not waste so much memory would be to seperate the number into powers of 10, 1000s etc. You could also probably do a recursive algorithm. For example with

12, 330, 032 Your first pass would seperate it into
12 330 032 and then
"twelve" "three hundred and thirty" "thirty two" and then combinding the subproblems

"twelve" million "three hundred and thirty" thousand and "thirty two"