how do you take an integer and outputed in words?

This is a discussion on how do you take an integer and outputed in words? within the C Programming forums, part of the General Programming Boards category; i need to write a program that takes an integer from -99 to +99 as input and outputs the number ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    how do you take an integer and outputed in words?

    i need to write a program that takes an integer from -99 to +99 as input and outputs the number in words. If the input is outside this range, the program should print that it is outside the range.

    I know there is a long way for this and a short way. Any guidance in which direction to go?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The way you find more understandable. If you understand them both, then go with the short way.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    well i would like to the learn how to do the short way but at the time i cant think of how to do it. anyone have a clue?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    Why not try a simpler program first? Write a program that takes an integer from 0 to +9 as input and outputs the number in words. If the input is outside this range, the program should print that it is outside the range.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I presume by the "long way", you mean a list of all numbers 0..99 written in words "zero", "one", ... "ninetynine". That's obviously feasible for a range of 0..99 (and in fact, when I was doing automated telephone systems, we did have ALL numbers from zero to ninetynine as individual numbers, rather than putting them together from components).

    The shorter method involves mathematically splitting the input into tens and units. You need to use subtraction and modulo to solve that problem.

    For large numbers, if you want to be able to say 1234567 as "One Million Two Hundred And Thirtyfour Thousand Five Hundred And Sixty Seven", you'd obviously not want to have a table with nearly 1.25 million numbers listed as text, so you would HAVE to use some form of math to solve the problem. It is not terribly hard.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Setup two two dimensional arrays. The first array will hold strings representing "one" thru "nineteen". The second array will hold strings representing the tens from "ten", "twenty", thirty" thru "ninety".

    Next create a function, let's call it PrintStringsFromEquivalentNumeric. The decimal value to be converted is passed to this function. The function will first determine if the input numeric is less than zero. If so, it will print "Minus" and change the input numeric to an absolute value. Next the function will check to determine if the input numeric is zero. If so, print "zero" and return. Else check to determine if the input number is between one and nineteen. If so, print string from first array. Else if the input numeric is greater than nineteen, print string from second array (divide input numeric by 10) and print string from first array by modulo'ing the input number.

    The PrintStringsFromEquivalentNumeric should take no more than eight lines of code. Otherwise, you're probably off on a tangent.
    Last edited by BobS0327; 09-22-2008 at 08:42 AM. Reason: Correct my error of transposing array usage at end of second paragraph

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21