Q:

Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)

Write C/C++/Java code to convert given number into words.
eg:-
Input:
1234
Output:
One thousand two hundred thirty-four.
Input:
10
Output:
Ten
Does it require a complete switch case for digits 0 to 10.
Adding "teen" after every number name (eg: 14: four "teen".) from 14 to 19.
And than adding "ty" and the digits name for a number in the range 20 to 99.
And so on. 
I think there must be some far better approach for solving this.
C code is preferred.

A:

#include<iostream>
using namespace std;
void expand(int);
int main()
{
    int num;
    cout<<"Enter a number : ";
    cin>>num;
    expand(num);
}
void expand(int value)
{
    const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
    "eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
    "eighteen","nineteen"};
    const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
    "eighty","ninety"};

    if(value<0)
    {
        cout<<"minus ";
        expand(-value);
    }
    else if(value>=1000)
    {
        expand(value/1000);
        cout<<" thousand";
        if(value % 1000)
        {
            if(value % 1000 < 100)
            {
                cout << " and";
            }
            cout << " " ;
            expand(value % 1000);
        }
    }
    else if(value >= 100)
    {
        expand(value / 100);
        cout<<" hundred";
        if(value % 100)
        {
            cout << " and ";
            expand (value % 100);
        }
    }
    else if(value >= 20)
    {
        cout << tens[value / 10];
        if(value % 10)
        {
            cout << " ";
            expand(value % 10);
        }
    }
    else
    {
        cout<<ones[value];
    }
    return;
}

A:

Instead of switch statements, consider using tables of strings indexed by a small value.
const char * const ones[20] = {"zero", "one", "two", ..., "nineteen"};
const char * const tens[10] = {"", "ten", "twenty", ..., "ninety"};

Now break the problem into small pieces.  Write a function that can output a single-digit number.  Then write a function that can handle a two-digit number (which will probably use the previous function).  Continue building up the functions as necessary.
Create a list of test cases with expected output, and write code to call your functions and check the output, so that, as you fix problems for the more complicated cases, you can be sure that the simpler cases continue to work.

