63 080
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #25691 in Other Programming & Computer Science for Sam

Question #25691
QUESTION 1
Write a practical Java program that reads an integer between 0 and 99 and verbalizes it. For example, if the program is given 23 it would display "twenty three".
Expert's answer
import java.util.Scanner;

public class Main
{
private final static String[] digits = { "one", "two", "three",
"four", "five", "six",
"seven", "eight", "nine" };
private final static String[] teens = { "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen",
"nineteen" };
private final static String[] tens = { "twenty", "thirty", "forty",
"fifty", "sixty", "seventy",
"eighty", "ninety" };

private static String verbalize(int number)
{
if (number == 0)
return "zero";
if (number < 10)
return digits[number - 1];
if (number < 20)
return teens[number - 10];

String result = tens[number / 10 - 2];
int digit = number % 10;

if (digit == 0)
return result;
return result + " " + digits[digit - 1];
}

public static void main(String[] args)
{
int number = new Scanner(System.in).nextInt();;
if (number < 0 || number > 99)
System.out.println("Wrong number");
else
System.out.println(verbalize(number));
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions