Print a number in words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vvsrinivasnalamati
    New Member
    • Mar 2010
    • 1

    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]
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Could you show us what you have so far? Are you doing this in a function?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      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
      Code:
      switch(n){
      case: 0;
      cout<<"zero";break;
      // the remaining cases follow on here 
      }
      You could also use
      Code:
      if(n==1)fprint("one");
           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

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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

        Comment

        Working...