Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   how to print any number in words (http://www.programmingforums.org/showthread.php?t=13832)

amitpansuria Aug 23rd, 2007 12:02 AM

how to print any number in words
 
helo i want to implement the following concept in my project
write a c/c++ algorithm for : accept a number from the user not greater than 6 digits and display the number in words i.e. if the input from the user is 18265 then the output should be Eighteen Thousand Two Hundred Sixty Five. if the input it 1187293 then the output should be Eleven Lac Eighty Seven Thousand Two Hundred Ninety Three.

how i iplement this
Regards,
Amit

lectricpharaoh Aug 23rd, 2007 1:25 AM

This looks like homework to me, but basically all you need is a lookup table containing strings. You'll need unique strings for 'zero' through 'nineteen', as well as the tens values ('twenty', 'thirty', etc). Then you'll need magnitude strings, such as 'hundred', 'thousand', 'million', and so on. If the number is an integer, that's about it, but if it's floating-point, you'll need 'point' in your lookup table.

From these, you can build up whatever number you want, assuming you don't exceed your maximum magnitude. You might also consider adding in 'and' where applicable. Though it's technically not correct usage, many people will say 'eight hundred and seventeen' rather than 'eight hundred seventeen'.

Final hint: the division and modulus operators are your friends for this endeavor.

Booooze Aug 23rd, 2007 1:58 AM

Quote:

Originally Posted by lectricpharaoh (Post 132681)
Final hint: the division and modulus operators are your friends for this endeavor.

Yeah, I as going to say that. I did a program similar to this with Roman Numerals. It was a real bitch. I never would have figured out that the modulus operator played such a big part in it, had someone not told me.

lectricpharaoh Aug 23rd, 2007 5:32 AM

Quote:

Originally Posted by Booooze
Yeah, I as going to say that. I did a program similar to this with Roman Numerals. It was a real bitch. I never would have figured out that the modulus operator played such a big part in it, had someone not told me.

Now I'm gonna have to do a class that can convert a number to both words and Roman numerals.

Did you know four can be written as 'IIII' as well as 'IV'? I expect the former would be easier to code for, but since you need the subtractive version for higher numbers, you need to code it anyways.

And what about fractions? Now those are annoying in the Roman system.

DaWei Aug 23rd, 2007 7:41 AM

I made a money-order dispenser that did this in English and Mexican Spanish. I learned the hard way that what used to be diez y seis is now deciseis and veinte y cuatro is veinticuatro. Somewhat of a bear.


All times are GMT -5. The time now is 4:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC