Number to Word

Enter a number within range 0 to 9999
4598
four thousand five hundred ninety eight
import java.io.*;
class Num2Word
 {
     public static void main()throws IOException
     {
        InputStreamReader x=new InputStreamReader(System.in);
        BufferedReader y=new BufferedReader(x);
        System.out.println(“Enter a number within range 0 to 9999”);
        int n=Integer.parseInt(y.readLine());
        int th,h,ut,u,t;
        String s[]={“”,”one”,”two”,”three”,”four”,”five”,”six”,”seven”,”eight”,”nine”,”ten”,”eleven”,
                        “twelve”,”thirteen”,”fourteen”,”fifteen”,”sixteen”,”seventeen”,”eighteen”,”nineteen”,
                        “twenty”,”thirty”,”forty”,”fifty”,”sixty”,”seventy”,”eighty”,”ninety”};
        if(n==0)
        System.out.println(“zero”);
        else
        {
            th=n/1000;
            if(th>0)
            System.out.print(s[th]+” “+”thousand”);
            n=n%1000;
            h=n/100;
            if(h>0)
            System.out.print(” “+s[h]+” “+”hundred”);
            ut=n%100;
            if(ut<=20)
            System.out.print(” “+s[ut]);
            else
             {
                 u=ut%10;
                 t=ut/10;
                 System.out.print(” “+s[18+t]+” “+s[u]);
             }
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s