-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFigureInWord
48 lines (45 loc) · 2.42 KB
/
FigureInWord
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
import java.util.Scanner;
public class FigureInWord {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
FigureInWord fiw = new FigureInWord();
System.out.print("Enter a number to convert to words (or 'done' to quit):");
String input = in.nextLine();
while (!input.equals("done")) {
try {
int inpt =Integer.parseInt(input);
System.out.print("The value is :" + convert(inpt));
System.out.println();
System.out.print("Enter a number to convert to words (or 'done' to quit):");
input = in.nextLine();
// System.out.print("The value is :" + convert(inpt));
} catch (Exception e) {
System.out.println("You entered a wrong value.");
}
}
System.out.println("Thank you for using our service.");
}
//Array Declarations for public
public static String[] zero_to_nineteen = {"Zero","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","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
//Morse code. Program kernel
public static String convert(int i) {
String sign = "Negative ";
if (i<0) {
i *=-1;
if( i < 20) return sign + zero_to_nineteen[i];
if( i < 100) return sign + tens[i/10] + ((i % 10 > 0)? " " + convert(i % 10):"");
if( i < 1000) return sign + zero_to_nineteen[i/100] + " Hundred" + ((i % 100 > 0)?" and " + convert(i % 100):"");
if( i < 1000000) return sign + convert(i / 1000) + " Thousand " + ((i % 1000 > 0)? " " + convert(i % 1000):"") ;
if(i<1000000000) return sign + convert(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convert(i % 1000000):"") ;
return (i / 1000000000) + " Billion " + ((i % 1000000000 > 0)? " " + convert(i % 1000000000):"") ;
} else {
if( i < 20) return zero_to_nineteen[i];
if( i < 100) return tens[i/10] + ((i % 10 > 0)? " " + convert(i % 10):"");
if( i < 1000) return zero_to_nineteen[i/100] + " Hundred" + ((i % 100 > 0)?" and " + convert(i % 100):"");
if( i < 1000000) return convert(i / 1000) + " Thousand " + ((i % 1000 > 0)? " " + convert(i % 1000):"") ;
if(i<1000000000) return convert(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convert(i % 1000000):"") ;
return (i / 1000000000) + " Billion " + ((i % 1000000000 > 0)? " " + convert(i % 1000000000):"") ;
}
}
}