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
when i send the request to the JSP page it will print as it is and why? and how to solve this problem please inform me that solution
What is the purpose of jdk?
What is a pojo class in java?
What is meant by pass by reference and pass by value in java?
What is scrollable resultset in java?
Which is more secure: java or activex? : java security
What are tlds in java?
What is log4j in java?
What are tags in java?
How do you run an executable jar file?
What is lsdou? : java security
What is stateless and stateful in java?
What is explicit casting?
What is dto layer in java?
What is lambda expression in mvc?