Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 a stream in programming?

862


How should I format my code? How should I comment my code?

982


What is @resource annotation in java?

915


What is a java singleton?

963


What is jdk jre and jvm?

998


Can you compile java into exe?

1041


What is the resourcebundle class?

927


What do I need to install netbeans?

1000


Are jvm’s platform independent?

911


What is lambda expressions action func and predicate?

976


In java what is the difference between sleep() and wait() .

972


What is transaction management in java?

909


What are tags in java?

890


What is an actionevent in java?

998


What services can invoke lambda?

914