In: Computer Science
Java Language
The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five".
Test Cases
Test case #1
Expected result: When t is 2, your code sets inwords to "two"
Test case #2
Expected result: When t is 50, your code sets inwords to "fifty"
Test case #3
Expected result: When t is 37, your code set inwords to "thirty seven"
Test case #4
Expected result: When t is 16, your code sets inwords to "sixteen"
JAVA CODE :
import java.util.*;
public class Main
{
public static String singles(int n) // return singles
{
if(n == 1) return "one";
if(n == 2) return "two";
if(n == 3) return "three";
if(n == 4) return "four";
if(n == 5) return "five";
if(n == 6) return "six";
if(n == 7) return "seven";
if(n == 8) return "eight";
return "nine";
}
public static String doubles2(int n) // returns after twnetys
{
if(n == 3) return "thirty";
if(n == 4) return "forty";
return "fifty";
}
public static String doubles1(int n) // returns elevenes, twelves,
..
{
if(n == 11) return "eleven";
if(n == 12) return "twelve";
if(n == 13) return "thirteen";
if(n == 14) return "forteen";
if(n == 15) return "fifteen";
if(n == 16) return "sixteen";
if(n == 17) return "seventeen";
if(n == 18) return "eighteen";
if(n == 19) return "nineteen";
return "twenty";
}
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.print("Enter the number
(1 to 50): ");
int n = s.nextInt(); // read teh
integer
if(n == 10)// if it is ten
System.out.println("Ten");
}
else if(n > 10 && n
<= 20)
{
System.out.println(doubles1(n));
}
else{
int n1 = n % 10;
int n2 = (n / 10) % 10;
if(n2 == 0)
{
System.out.println(singles(n1));
}
else{
if(n2 == 2)
{
System.out.println(doubles1(n2) + "
" + singles(n1));
}
else{
System.out.println(doubles2(n2) + "
" + singles(n1));
}
}
}
}
}
SAMPLE OUTPUTS :