-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathNumberToWords.java
90 lines (88 loc) · 3.19 KB
/
NumberToWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package javacodes;
/*Given a number, you have to write it in words*/
import java.io.*;
import java.util.*;
//number to word
class NumberToWords {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(System.in);
int t, l, x, y;
String s, ans;
// initializing the strings to store the words for each number
String l19[] = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
String l90[] = {"", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
//System.out.println("Enter input:");
t = Integer.parseInt(in.readLine()); //taking number as input
while (t > 0) {
ans = "";
int c = 12;
s = in.readLine();
l = s.length();
int n[] = new int[13];
for (int i = l - 1; i >= 0; i--, c--) {
n[c] = s.charAt(i) - 48; //extracting character and converting character to integer
}
y = 0;
for (int i = ++c; i < 13; i++) {
switch (i) { //switch case
case 0:
if (n[i] == 0) {
break;
}
ans = ans + l19[n[i]] + "Trillion ";
break;
case 1:
case 7:
case 4:
case 10:
if (n[i] == 0) {
break;
}
ans = ans + l19[n[i]] + "Hundred ";
y = n[i];
break;
case 2:
case 5:
case 8:
case 11:
x = n[i] * 10 + n[i + 1];
y = y * 100 + x;
if (y == 0) {
break;
}
if (x < 20) {
ans = ans + l19[x];
} else {
ans = ans + l90[n[i]] + l19[n[i + 1]];
}
i++;
break;
case 3:
case 6:
case 9:
case 12:
y = n[i];
if (y == 0) {
break;
}
ans = ans + l19[y];
break;
}
if (i - 1 == 2 && y != 0) {
ans = ans + "Billion ";
y = 0;
}
if (i - 1 == 5 && y != 0) {
ans = ans + "Million ";
y = 0;
}
if (i - 1 == 8 && y != 0) {
ans = ans + "Thousand ";
y = 0;
}
}
System.out.println(ans.trim());
t--;
}
}
}