Write a program that accepts a number and output its equivalent in words.
Sample input/output:
Codes:
package javaapplication1;
import java.io.*;
public class num1 {
public static int charAt(int num, int index) {
return String.valueOf(num).charAt(index); }
private static PrintStream sys = System.out;
public static BufferedReader q = new BufferedReader (new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
String[] units = {null, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String[] tenths = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
String[] special = {null, null, "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String[] hundreds = {null, "one hundred", "two hundred", "three hundred", "four hundred", "five hundred",
"six hundred ", "seven hundred", "eight hundred", "nine hundred"};
String[] thousands = {null, "one thousand", "two thousand", "three thousand", "four thousand",
"five thousand","six thousand", "seven thousand", "eight thousand", "nine thousand"};
int y = 0, num = 0;
do {
sys.print("Enter a number: "); num = Integer.parseInt(q.readLine());
if (num < 10000)
y = 1;
} while (y==0);
int x = Integer.toString(num).length();
switch (x)
{
case 1: {
if (num == 0)
sys.println("zero");
else
sys.println(units[num]);
break; }
case 2: {
if (charAt(num,0) - '0' == 1)
sys.println(tenths[charAt(num,1) - '0']);
else
{
sys.print(special[charAt(num,0) - '0']+" ");
sys.println(units[charAt(num,1) - '0']);
}
break; }
case 3: {
sys.print(hundreds[charAt(num,0) - '0']+" ");
{
if (charAt(num,1) - '0' == 1)
sys.print(tenths[charAt(num, 2) - '0']);
else if (charAt(num,1) - '0' == 0)
{ if (charAt(num, 2) - '0' != 0)
sys.print(units[charAt(num, 2) - '0']); }
else
{
sys.print(special[charAt(num, 1) - '0'] + " ");
if (charAt(num,2) - '0' != 0)
sys.print(units[charAt(num,2) - '0']);
}
}
sys.println();
break; }
case 4: {
sys.print(thousands[charAt(num,0) - '0']+" ");
if (charAt(num,1) - '0' != 0)
sys.print(hundreds[charAt(num,1) - '0']+" ");
{
if (charAt(num,2) - '0' == 1)
sys.print(tenths[charAt(num, 3) - '0']);
else if (charAt(num,2) - '0' == 0)
{ if (charAt(num, 3) - '0' != 0)
sys.print(units[charAt(num, 3) - '0']); }
else
{
sys.print(special[charAt(num, 2) - '0'] + " ");
if (charAt(num,3) - '0' != 0)
sys.print(units[charAt(num,3) - '0']);
}
}
sys.println();
break; }
}
}
}
0 comments:
Post a Comment