-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFigureInWords.java
More file actions
195 lines (148 loc) · 4.42 KB
/
FigureInWords.java
File metadata and controls
195 lines (148 loc) · 4.42 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.util.Scanner;
public class FigureInWords{
public static Scanner in = new Scanner(System.in);
public static String [] units = {"", " one ", " two ", " three ", " four ", " five ", " six ", " seven ", " eight ",
" nine ", " ten ", " eleven ", " twelve ", " thirteen ", " fourteen ", " fifteen ",
" sixteen ", " seventeen ", " eighteen ", " nineteen " } ;
public static String [] tens = {"", ""," twenty ", " thirty ", " fourty ", " fifty ", " sixty ", " seventy ", " eighty ", " ninty "};
public static String [] big = {" hundred ", " thousand ", " million ", " billion "};
public static int unit, ten, hundred, thousand,tenThousand, tenMillion, hundredThousand, million, toTenMillion, hundredMillion, toHundredMillion, billion, tenUnit, tohundred, toThousand, toTenThousand, toHundredThousand, toMillion;
public static String word = "",s = "", str = "hundred ";
public static int [] n = {0, 10, 100 ,1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
public static void main(String[] args) {
System.out.println("Enter a number to convert to words (or 'done' to quit):");
String numb = in.nextLine();
while(!(numb.equals ("done"))) {
try {
int num = Integer.parseInt(numb);
init(num);
}
catch (NumberFormatException ex){
System.out.println("You have Entered the worng value");
};
System.out.println("Enter a number to convert to words (or 'done' to quit):");
numb = in.nextLine();
word="";
s="";
};
System.out.println(" Thank you for using our service");
}
public static void init(int num ){
int digit = 0;
int temp = num;
while(temp>0)
{
if (temp%10 >= 0){
digit++;
temp = temp/10;
};
};
if (num < 0) {
num *= -1;
s = "negative ";
}
if (num == 0) System.out.println("Zero");
if (num < 20){
word = units[num];
}
if (digit == 2){
tens(num);
}
if (digit == 3){
hundreds(num);
}
if (digit == 4){
thousands(num);
}
if (digit == 5){
tenThousands(num);
}
if (digit == 6){
hundredThousands(num);
}
if (digit == 7){
millions(num);
}
if (digit == 8){
tenMillions(num);
}
if (digit == 9){
hundredMillions(num);
}
if (digit == 10){
billions(num);
}
System.out.println(s+word);
}
public static String tens(int num) {
if (num > 20){
ten = num/n[1];
unit = num%n[1];
word =tens[ten]+""+units[unit];
};
return word;
}
public static String hundreds(int num) {
hundred = num/n[2];
tenUnit= num%n[2];
if (tenUnit != 0) str = "hundred and";
tens(tenUnit);
if(tenUnit == 0) str = "";
word = units[hundred] +str+ word;
return word;
}
public static String thousands(int num) {
thousand = num/n[3];
tohundred = num %n[3];
hundreds(tohundred);
if(thousand == 0) str = "";
word = units[thousand] + big[1] +word;
return word;
}
public static String tenThousands(int num) {
tenThousand = num/n[4];
toThousand = num % n[4];
thousands(toThousand);
word = tens[tenThousand]+word;
return word;
}
public static String hundredThousands(int num) {
hundredThousand = num/n[5];
toTenThousand = num % n[5];
if (toTenThousand != 0) str = "hundred and";
if(toTenThousand == 0) str = "";
tenThousands(toTenThousand);
word = units[hundredThousand]+ str +word;
return word;
}
public static String millions(int num) {
million = num/n[6];
toHundredThousand = num % n[6];
hundredThousands(toHundredThousand);
word = units[million] +big[2] +word;
return word;
}
public static String tenMillions(int num) {
tenMillion = num/n[7];
toMillion = num % n[7];
millions(toMillion);
word = tens[tenMillion] +word;
return word;
}
public static String hundredMillions(int num) {
hundredMillion = num/n[8];
toTenMillion = num % n[8];
tenMillions(toTenMillion);
if (toTenMillion != 0) str = "hundred and";
if(toTenMillion == 0) str = "";
word = units[hundredMillion] +str +word;
return word;
}
public static String billions(int num) {
billion = num/n[9];
toHundredMillion = num %n[9];
hundredMillions(toHundredMillion);
word = units[billion] + big[3] +word;
return word;
}
}