wap to input a natural number less than and display it in
words. test your program on the sample data and some random
data
sample input= 29
sample output= Twenty nine
sample input=17001
sample output= out of range
sample input=119
sample output =one hundred and nineteen
sample input=500
sample output=five hundred
Answer / megha
import java.io.*;
class Program1
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str1="",str2="";
int h=0,t=0,u=0,i=0,n;
public void takeNumber() throws Exception
{
System.out.println("\nEnter the number:");
n=Integer.parseInt(br.readLine());
if(n<1 || n>999)
{
System.out.println("\nINVALID ENTRY:");
return;
}
while(true)
{
if(i==0)
u=n%10;
else if(i==1)
t=n%10;
else if(i==2)
h=n%10;
i++;
n=n/10;
if(n==0)
break;
}
if(h!=0)
str=word1(h)+ " HUNDRED ";
if(t>1)
str1= word2(t);
if(t==1)
str2= word3(u);
else
str2=word1(u);
if(!str1.equals("") || !str2.equals(""))
str=str+ "AND ";
if(!str1.equals(""))
str=str+ str1+ " ";
if(!str2.equals(""))
str=str+ str2;
System.out.println(str);
}
String word1(int x)
{
String s="";
switch(x)
{
case 1:
s="ONE";
break;
case 2:
s="TWO";
break;
case 3:
s="THREE";
break;
case 4:
s="FOUR";
break;
case 5:
s="FIVE";
break;
case 6:
s="SIX";
break;
case 7:
s="SEVEN";
break;
case 8:
s="EIGHT";
break;
case 9:
s="NINE";
break;
}
return s;
}
String word2(int x)
{
String s="";
switch(x)
{
case 2:
s="TWENTY";
break;
case 3:
s="THIRTY";
break;
case 4:
s="FOURTY";
break;
case 5:
s="FIFTY";
break;
case 6:
s="SIXTY";
break;
case 7:
s="SEVENTY";
break;
case 8:
s="EIGHTY";
break;
case 9:
s="NINETY";
break;
}
return s;
}
String word3(int x)
{
String s="";
switch(x)
{
case 0:
s="TEN";
break;
case 1:
s="ELEVEN";
break;
case 2:
s="TWELVE";
break;
case 3:
s="THIRTEEN";
break;
case 4:
s="FOURTEEN";
break;
case 5:
s="FIFTEEN";
break;
case 6:
s="SIXTEEN";
break;
case 7:
s="SEVENTEEN";
break;
case 8:
s="EIGHTEN";
break;
case 9:
s="NINETEEN";
break;
}
return s;
}
public static void main(String args[]) throws Exception
{
Program1 ob=new Program1();
ob.takeNumber();
}
}
| Is This Answer Correct ? | 2 Yes | 0 No |
What is difference between J2SE,J2EE and J2ME?
How can I create connection two blue-tooth mobile in j2me. please urgent
What is preverifier?
Explain Virtual Machine for J2ME?
What is minimum hardware and Software requirements for J2ME?
What is Java Community Process (JCP)?
What is CDC?
What is Generic" J2ME architecture?
List some Property Supported by J2ME?
What is Telematics?
What hardware and software requirements of a device that intends to implement the MIDP.
What are profiles in J2ME?