Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,178 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,002 people online right now. Registration is fast and FREE... Join Now!




how to display any number(up to 6 digit) in words

 
Reply to this topicStart new topic

how to display any number(up to 6 digit) in words

amitpansuria
22 Aug, 2007 - 08:24 PM
Post #1

New D.I.C Head
*

Joined: 7 Aug, 2007
Posts: 5


My Contributions
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
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Display Any Number(up To 6 Digit) In Words
22 Aug, 2007 - 08:28 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You would run it through a logical parser.
I would dump the digit to an array buffer, so each number is in a slot, like so

say, 123456 is the given number,

CODE
sprintf(buffer,"%d",digit);
printf("%s\n",buffer);

Buffer should exist like this: buffer[1][2][3][4][5][6][\0];
So, then you would use logic to figure how large the number is, then use the english words accordingly.
User is online!Profile CardPM
+Quote Post

dlkj
RE: How To Display Any Number(up To 6 Digit) In Words
22 Aug, 2007 - 09:56 PM
Post #3

D.I.C Head
**

Joined: 11 Mar, 2002
Posts: 137

you have to recognize a few things
1) hundred, thousand, million, etc are recursive. if you detect a number bigger than this you have to
a) return_string = recurse(value/1000); // handle bigger
B) return_string+= "thousand"; // output relevant string
c) return_string+= recurse(value%1000); // handle remainder
2) twenty, thirty, etc are differently handled
a) return_string = "twenty";
B) return_string+= recurse(value-20);
3) one till nineteen are different again
a) return_string = "nineteen"
4) zero is special.
a) handle zero inside recurse by terminating recursion silently
B) handle direct input of zero from user by return_string="zero" specially

use arrays of 'tuples' to help you mapping the values to the strings and also help you construct loops which will handle the cases (eg one till nineteen) without having to type out one if/else per case.
FYI, tuples are simple structures with two members.. you can have one string and one integer for example.

hope this helps.
I would construct this gradually. show us something that can handle one till nineteen.. get that working
after that, get the twenty till ninety nine working.
finally tackle the millions.

show us the code and we will help.

also, this is specific to english language. I wonder how the algorithm maps for different languages.

This post has been edited by dlkj: 22 Aug, 2007 - 09:56 PM
User is offlineProfile CardPM
+Quote Post

shetty
RE: How To Display Any Number(up To 6 Digit) In Words
6 Sep, 2007 - 03:56 AM
Post #4

New D.I.C Head
*

Joined: 6 Sep, 2007
Posts: 2


My Contributions
QUOTE(amitpansuria @ 22 Aug, 2007 - 09:24 PM) *

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


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:00AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month