Write a program to input a natural number less than 1000 and display it in words. Test your program on the sample data and some random data...

Write a program to input a natural number less than 1000 and display it in words. Test your program on the sample data and some random data.

Sample input and output of the program.

Examples

Input: 29
Output: TWENTY NINE

Input: 17001
Output: OUT OF RANGE

Input: 119
Output: ONE HUNDRED AND NINETEEN

Input: 500
Output: FIVE HUNDRED
import java.util.*;
class ISCprac2011q01{
public static void main(String args[])
throws InputMismatchException{

Scanner scan=new Scanner(System.in);

int n;

System.out.println("Enter a number less than 1000");
n=scan.nextInt();

if(n>=1000)
{
System.out.println("OUT OF RANGE");
}else{
String result,h="",t="",o="";
int a,b,c;
String ones[]={"one", "two","three","four","five",
"six","seven","eight","nine"};
String teens[]={"eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"};
String tens[]={"ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety"};

        if(n>=100 && n<1000){
             a=n/100;
            result=ones[a-1]+" hundred";
            n=n%100;
        }
        if(n%10==0 && n!=0){
        a=n/10;
        if(result!=null){
        result+=" and ";
        result+=tens[a-1];
        }else{
            result=tens[a-1];
        }
        n=n%10;
    }
        if(n>20 && n<100){
        a=n/10;
        b=n%10;
        if(result!=null){
        result+=" and ";
        result+=tens[a-1]+" "+ones[b-1];
        }else{
        result=tens[a-1]+" "+ones[b-1];
        }
    }
        if(n>10 && n<20){
        a=n%10;
       if(result!=null){
        result+=" and ";
        result+=teens[a-1];
        }else{
        result=teens[a-1];
        }
    }
        if(n<10 && n!=0){

        if(result!=null)
        {
            result+=" and ";
            result+=ones[n-1];
        }else{
            result=ones[n-1];
        }


        OR        


        a=n/100;
        if(n>100){
             n=n%100;
        }
        b=n/10;
        c=n%10;
        if(a!=0){
            h=ones[a-1]+" hundred";
        }
        if(b==0 && c!=0){
                o=ones[c-1];
            }
        else if(c==0 && b!=0){
            t=tens[b-1];
        }
        else if(b==1){
            t=teens[c-1];
        }
       else if(b!=0 && c!=0){
            t=tens[b-1]+" "+ones[c-1];
        }
        if(b==0 && c==0)
        result=h;
        else if(a==0)
        result=t+" "+o;
        else
        result=h+" and "+t+" "+o;

System.out.println("\n"+result.toUpperCase());

}
}
}        

Have something to say? Log in to comment on this post.

0 comments