-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[Others]Cheque.js
160 lines (141 loc) · 3.72 KB
/
[Others]Cheque.js
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
/*
Convert number to words
123456
Output: one hundred twenty three thousand four hundred fifty six
0 to 999 999 ONLY
*/
const convert = input => {
var ones = [
"",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirdteen",
"fourteen",
"fifthteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
];
var tens = [
"",
"",
"twenty",
"thirty",
"fourty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
];
if (input < 20) {
return ones[input];
}
//2 digits
if (input < 100) {
return (
tens[Math.floor(input / 10)] +
(input % 10 !== 0 ? " " : "") +
ones[input % 10]
);
}
//3 digits
if (input < 1000) {
return ones[Math.floor(input / 100)] + " Hundred " + convert(input % 100);
}
//4 & 5 digit
if (input < 100000) {
return (
convert(Math.floor(input / 1000)) + " Thousand " + convert(input % 1000)
);
}
if (input < 1000000) {
return (
convert(Math.floor(input / 100000)) +
" Hundred " +
convert(input % 100000)
);
}
};
var original = 999999;
console.log(convert(original));
/* IMPORTANT: Multiple classes and nested static classes are supported */
/*
* uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import for Scanner and other utility classes
import java.util.*;
*/
import java.util.*;
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
boolean numeric = input.matches("-?\\d+(\\.\\d+)?");
if(numeric){
String ans = helper(Integer.parseInt(input));
if(ans.length() < 1){
System.out.println("Invalid Input");
}
else{
ans = ans.substring(0,1).toUpperCase() + ans.substring(1) + " only";
System.out.println(ans);
}
}
else{
System.out.println("Error occurred");
}
}
public static String helper(int input){
String[] ones = {"", "one", "two", "three","four","five","six","seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String[] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String ans = "";
if(input < 20){
ans = ones[input];
}
else if(input < 100){
if(input % 10 == 0){
ans = ans + tens[input/10];
}
else{
ans = ans + tens[input/10] + " " + ones[input %10];
}
}
else if(input < 1000){
if(input %100 == 0){
ans = ones[input/100] + " hundred";
}
else{
ans = ones[input/100] + " hundred " + "and " + helper(input %100);
}
}
else if(input < 100000){
ans = helper(input/1000) + " thousand";
if(input % 1000 > 99){
ans = ans + ", ";
ans = ans + helper(input %1000);
}
else if( input % 1000 == 0){
return ans;
}
else{
ans = ans + " and " + helper(input%1000);
}
}
return ans;
}
}