If you have any problem with the code feel free to comment. The second image is unreadable so I have created the program according to my logic.
Program
import java.util.Scanner;
class NumberToWords {
private static final String[] digitNames = { "", "
thousand", " million", " billion", " trillion", "
quadrillion",
" quintillion"
};
private static final String[] tensNames = { "", "
ten", " twenty", " thirty", " forty", " fifty", " sixty",
" seventy", "
eighty", " ninety" };
private static final String[] oneToNineteenNames =
{ "", " one", " two", " three", " four", " five", " six",
" seven", "
eight", " nine", " ten", " eleven", " twelve", " thirteen", "
fourteen", " fifteen", " sixteen",
" seventeen", "
eighteen", " nineteen" };
private static String convertLessThanThousand(int
number) {
String word;
if (number % 100 < 20) {//
checking if the number is below 20
word =
oneToNineteenNames[number % 100];
number /=
100;
}
else {// when its not below
20
word =
oneToNineteenNames[number % 10];
number /=
10;
word =
tensNames[number % 10] + word;
number /=
10;
}
if (number == 0)//when number is
below hundred
return
word;
return oneToNineteenNames[number] +
" hundred" + word;//when number is above hundred
}
public static String convert(int number) {
if (number == 0) {//when number
is zero
return
"zero";
}
String word = "";
int place = 0;
//looping through the number and
finding its word form
do {
int n = number %
1000;
if (n != 0)
{
String s = convertLessThanThousand(n);
word = s + digitNames[place] + word;//for
thousands to billion
}
place++;
number /=
1000;
} while (number > 0);
return word.trim();
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
int num;
while (true) {
System.out.print("Enter the integer to pronounce(any negative value
to exit): ");
num =
sc.nextInt(); sc.nextLine();
if(num<0)
{
System.out.println("Bye");
break;
}
System.out.println(NumberToWords.convert(num));
System.out.println();
}
sc.close();
}
}
Output
![<terminated> Test (1) [Java Application] C:\Program FilesJava\jre1.8.0_211\bin\javaw.exe (31-Oct-2019, 9:11:26 am) Enter the](http://img.homeworklib.com/questions/5e6f55d0-a9a2-11ea-8eee-c5b53ead9187.png?x-oss-process=image/resize,w_560)
I need java code... Background Suppose you're working on the next great cligital assistant to compete...