467,169 Members | 962 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

Print a number in words

hi,i need a program that which prints the given number in words.
ie
if we enter 0123
the output will be as "ZERO ONE TWO THREE"[/b]
Mar 18 '10 #1
  • viewed: 2581
Share:
3 Replies
jkmyoung
Expert 2GB
Could you show us what you have so far? Are you doing this in a function?
Mar 18 '10 #2
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
Typically
Expand|Select|Wrap|Line Numbers
  1. switch(n){
  2. case: 0;
  3. cout<<"zero";break;
  4. // the remaining cases follow on here 
  5. }
  6.  
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
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
By using this site, you agree to our Privacy Policy and Terms of Use.