Re: Converting Numbers to Words in English by recursion.



On 9 Mar 2007 20:03:09 -0800, jedale@xxxxxxxxx wrote:

I am trying to convert numbers to there corresponding words but it
only works for numbers under 1,000 but need it to work for 1 billion.

Here is the code I have so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

/* Prototype */
void print_num(int num);

char *digits[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};

char *tens[] = {
"ten", "twenty", "thirty", "fourty", "fifty", "sixty",
"seventy", "eighty", "ninety"};

char *places[] = {"hundred", "thousand", "hundred", "million",
"hundred", "billion"};

int main(){

int num = 120394;

print_num(num);
printf("%d ", num); /* Print that number */

}

/* Print out the number sequence */
void print_num(int num){


if(num < 10){
printf("%s ", digits[num]);

}
else if(num < 100){
//print_num(num/10);
printf("%s ", tens[num/10]);
print_num(num%10 +1);
}

else if(num < 1000){
print_num(num/100);
printf("%s ", places[num/1000]);
print_num(num/10);

}else{


print_num(num/10);
print_num(num%10);

}
}
Numbers are split into groups of three digits: 123,456,908,436. Your
program needs to take account of that. What you have handles most
three digit combinations (000 to 999) in most situations. You need to
work on where to put the other words that describe the three digit
combinations, and to handle the exceptions to the standard wording of
some three digit groups.

123,000 = "one hundred and twenty three thousand" (drop the "and" if
you are American). That is 123 -> "one hundred and twenty three" plus
some extra verbiage for the 000 at the end. Now think about
123,000,000 and 123,000,000,000.

Extend to 123,456,000, 123,456,908 and 123,456,908,436.

Look for patterns and think about how to program them.

rossum

.



Relevant Pages

  • Re: Ex 7-5
    ... int getop; ... double num; ... void clearStack; ... not make sense if the stack has been cleared so a fall-though can't ...
    (comp.lang.c)
  • Re: Ex 7-5
    ... void clearStack; ... I don't interpret the suggestion to rewrite to use scanf ... // Tondo and Gimpel has the next declaration wrongly an int ... double num; ...
    (comp.lang.c)
  • Re: Ex 7-5
    ... int getop; ... double num; ... allowed me to get whatever stack values I wanted in the first ... void clearStack; ...
    (comp.lang.c)
  • Re: code for two half diamond shapes
    ... > void space(int num); ... > int num; ... Quote what you're replying to -- even when replying to yourself, ...
    (comp.lang.c)
  • Re: pgm without std library functions
    ... Depending on your definition of read, ... int main (void) { ... read(STDIN_FILENO, &num, 1); ...
    (comp.lang.c)