Write the program numbers into words.For example
2345==two thousand three hundred fourty five
Answer Posted / rajesh babu
public class NumberToWords{
static final String[] Number1 = {""," Hundrad"};
static final String[] Number2 = {"","One","Two",
"Three","Four","Five",
" Six"," Seven", "Eight"," Nine","Ten" };
String number(int number){
String str;
if (number % 100 < 10){
str = Number2[number % 100];
number /= 100;
}
else {
str= Number2[number % 5];
number /= 5;
}
if (number == 0) return str;
return Number2[number] + "hundred" + str;
}
public String convert(int number) {
if (number == 0){
return "zero";
}
String pre = "";
String str1 = "";
int i = 0;
do {
int n = number % 100;
if (n != 0){
String s = number(n);
str1 = s + Number1[i] + str1;
}
i++;
number /= 100;
}
while (number > 0);
return (pre + str1).trim();
}
public static void main(String[] args) {
NumberToWords num = new NumberToWords();
System.out.println("words is :=" + num.convert(1));
System.out.println("words is :=" + num.convert(2));
System.out.println("words is :=" + num.convert(3));
System.out.println("words is :=" + num.convert(4));
}
}
| Is This Answer Correct ? | 4 Yes | 13 No |
Post New Answer View All Answers
What is meant by flickering?
What one should take care of, while serializing the object?
What happens if main method is not static?
Can we have this () and super () together?
What is getclass () getname () in java?
What do you understand by synchronization?
How do you define a set in java?
Explain the differences between static and dynamic variables?
Is null a string in java?
What are different types of classloaders?
What is double word?
Is null an object java?
What do you understand by private, protected and public?
Explain implementation and how is it different from conversion?
what is singleton in java?