Input :14000 Output : hundred and fourteen thousand.(wrong)
Desired output : fourteen hundred thousand.

Answer Posted / anubhav gupta

public class ToText {

int num,length;
boolean length_odd;

//determine the length of a given number
int getlength(int x)
{
int temp=x,len=0;
while(temp!=0)
{
temp=temp/10;
len++;
}
return len;

}

//constructor
ToText(int no)
{
num=no;
length=getlength(num);
if(num==0)
{System.out.println("zero"+ length);
length--;
}
theString(num);


}

//naming every number from 0 to 20 and we can construct
//the rest of them easily
void getname(int x)
{

switch(x)
{
case 0:System.out.print( " ");
break;
case 1: System.out.print( " one");break;
case 2: System.out.print( " two");break;
case 3 : System.out.print( " three");break;
case 4 : System.out.print( " four");break;
case 5 : System.out.print( " five");break;
case 6: System.out.print( " six");break;
case 7: System.out.print( " seven");break;
case 8: System.out.print( " eight");break;
case 9 : System.out.print( " nine");break;
case 10 : System.out.print( " ten");break;
case 11 : System.out.print( " eleven");break;
case 12 : System.out.print( " twelve");break;
case 13: System.out.print( " thirteen");break;
case 14: System.out.print( " fourteen");break;
case 15 : System.out.print( " fifteen");break;
case 16: System.out.print( " sixteen");break;
case 17: System.out.print( " seventeen");break;
case 18: System.out.print( " eighteen");break;
case 19: System.out.print( " nineteen");break;
case 20: System.out.print( " twenty");break;
case 30: System.out.print( " thirty");break;
case 40: System.out.print( " fourty");break;
case 50: System.out.print( " fifty");break;
case 60: System.out.print( " sixty");break;
case 70: System.out.print( " seventy");break;
case 80: System.out.print( " eighty");break;
case 90: System.out.print( " ninety");break;
default:break;


}
}

//no of digit from right side,(counting starts from 1
and not 0)
void gettxt(int x)
{
switch(x)
{
case 3:System.out.print(" hundred");break;
case 4:System.out.print(" thousand");break;
case 6:System.out.print(" lakh");break;
case 8:System.out.print(" crore");break;
case 10:System.out.print(" arab");break;
default:break;

}
}

//Main Logic--start from left if length is odd them
//fetch two digits
//from left and convert them else if length is even
//fetch one digit
//from left and convert it.eg: 12345, it's length is 5
//which is odd
//it will fetch 12 and convert it to text.eg: 2,345
//it's length is even
//therefore it will fetch 2 and convert it.
//for the last three digit ...
//first we will fetch the third last digit only and
//convert it.
//then we will fetch the last to digit together and
//convert them.
//also consider the case when user enters 0.

void theString(int x)
{



if(length<=0)
System.exit(0);

int temp,temp1;
if(length%2==1)
length_odd=true;
else length_odd=false;

//last three digit logic

if((length==3)||(length==1))
length_odd=false;
else if(length==2)
length_odd=true;

//---------
if(length_odd)
{
temp=x/(int)(Math.pow(10,length-2));
if(temp>20)
{
temp1=temp%10;
temp=temp/10;
temp =temp*10;
if((temp1==0)&&(temp==0)){length-=2;}
else{
getname(temp);
getname(temp1);
length--;
gettxt(length);
length--;
}
}
else
{
if(temp==0){length-=2;}
else{
getname(temp);
length--;
gettxt(length);
length--;
}
}

num=num%(int)(Math.pow(10,length));

}

else if(length_odd==false)
{
temp=x/(int)(Math.pow(10, length-1));
if(temp==0)
{}
else{
getname(temp);
gettxt(length);
}
length--;
num=num%(int)(Math.pow(10,length));
}


theString(num);



}



public static void main(String[] args)
{
ToText t=new ToText(1234);



}
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the resourcebundle class?

530


What is meant by rest api in java?

468


what is reflection api? How are they implemented?

532


What is setstring method in java?

505


What is war file in java?

570






When a thread is created and started, what is its initial state?

549


What if I do not provide the string array as the argument to the method?

496


Is java built on c?

493


What about products that claim to detect malicious applets? : java security

468


What is scrollable resultset in java?

475


How do I open my java console?

493


program A and B are analysed and found to have worst case running time greater than 150NlogN and N*N respectively. which program has the better guarantee after the running time for the large values of N(N>10000)? which program has the better guarantee for the running time of small program N (N<100)? which program will run faster on average for N=1000?

1599


Why java?

532


What's the difference between code-based security and role-based security? Which one is better? : java security

499


Topic- looping,function overloading,nesting ,polymorphism. Aim - to write a function with a name buzz-buds,that will check whether the given numbers are buddies or not on the basis of no. of parameters passed during function calling.

1600