470,643 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

Print a number in words

hi,i need a program that which prints the given number in words.
if we enter 0123
the output will be as "ZERO ONE TWO THREE"[/b]
Mar 18 '10 #1
3 2719
2,057 Expert 2GB
Could you show us what you have so far? Are you doing this in a function?
Mar 18 '10 #2
542 512MB
There are a number of ways of tackling this; one way is to use switch(n) with
case: 0: case:1 //etc through 9
Expand|Select|Wrap|Line Numbers
  1. switch(n){
  2. case: 0;
  3. cout<<"zero";break;
  4. // the remaining cases follow on here 
  5. }
You could also use
Expand|Select|Wrap|Line Numbers
  1. if(n==1)fprint("one");
  2.      else if(n==2)fprint("two");
In your case you might place your individual digits of the integer in a container such as an array or vector and then use a for loop to direct each array element to the switch() and case: statements
Mar 24 '10 #3
2,425 Expert 2GB
Your example, where a leading zero is recognized, means that the input is a string of digit characters rather than an integer. So your task is to print an output string corresponding to each character of the input string. You need to:
  • Traverse the characters of the input string
  • For each input character
    • Decide which output string is appropriate
    • Print that output string
Mar 24 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

21 posts views Thread by arnuld | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.