program to convert numbers into words

This is a discussion on program to convert numbers into words within the C Programming forums, part of the General Programming Boards category; hi all, I was trying to write a program that converts numeric figures into words i.e. 342 gets printed as ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    program to convert numbers into words

    hi all,

    I was trying to write a program that converts numeric figures into words i.e. 342 gets printed as three hundred forty two.

    I have reached the stage where i can convert any number like 342 into three four two but not three hundred forty two with the following code, any suggestions?

    Code:
    main()
    {
    unsigned int num,num2,no_dig=0,p;
    int i;
    
    printf("\nEnter any positive integer : ");
    scanf("%d",&num);
    
    do
    {
    no_dig++;
    num2=num2/10;
    } while(num2!=0);
    
    for(;no_dig>0;no_dig--,num%=p)
    {
    p=1;
    for(i=no_dig-1;i>0;i--)
    p=p*10;
    
    switch(num/p)
    {
    
    case 0:
    printf("ZERO");
    break;
    
    case 1:
    printf("ONE");
    break;
    
    case 2:
    printf("TWO");
    break;
    
    case 3:
    printf("THREE");
    break;
    
    case 4:
    printf("FOUR");
    break;
    
    case 5:
    printf("FIVE");
    break;
    
    case 6:
    printf("SIX");
    break;
    
    case 7:
    printf("SEVEN");
    break;
    
    case 8:
    printf("EIGHT");
    break;
    
    case 9:
    printf("NINE");
    break;
    
    }
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a lookup table for every number there is...


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    >Make a lookup table for every number there is...

    ROFLCOPTER!
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    int n = 342;
    char *c = (char*)n;
    printf("%s", c);

    Ok, so that doesn't work.

    Think. Given a number like 342, how would YOU convert it into an ordinal? Invent a step-by-step way to do it, and translate it into code.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I wonder if the search feature still works.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    yes I found the code, thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 7
    Last Post: 01-30-2006, 01:39 AM
  3. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

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