forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToWord.java
More file actions
166 lines (153 loc) · 7.03 KB
/
IntegerToWord.java
File metadata and controls
166 lines (153 loc) · 7.03 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
/* This program converts numbers into its word counterparts.
This can only handle integer inputs from -999 to 999.
*/
import java.util.Scanner;
public class IntegerToWord {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a # (bet. -999 to 999): ");
int submit = s.nextInt(); // User Input
int num = Math.abs(submit); // Turns negative input to Positive Input
int hundreds = num / 100; // Gets the left most digit (hundreds) of the Input
int ones = num % 10; // Gets the right most digit (ones) of the Input
int digitMiddleRight = num % 100; // Gets the Middle digit (tens) and Right Digit (ones).
int tens = digitMiddleRight / 10; // It is now possible to get the middle digit (tens).
if (num >= -999 && num <= 999) { // Input must be within this range.
if (submit < 0) // Checks if USER INPUT is negative.
{
System.out.print("Negative "); // Prints NEGATIVE. The first word if the USER INPUT is Negative.
} else if (num == 0) // If Input is 0. then Zero
{
System.out.println("Zero");
} else {
System.out.print(""); // If Input is Positive. Just leave blank.
}
if (hundreds >= 1) // Remember that int hundreds was used to get the left most digit.
{
switch (hundreds) {
case 1:
System.out.print("One hundred ");
break;
case 2:
System.out.print("Two hundred ");
break;
case 3:
System.out.print("Three hundred ");
break;
case 4:
System.out.print("Four hundred ");
break;
case 5:
System.out.print("Five hundred ");
break;
case 6:
System.out.print("Six hundred ");
break;
case 7:
System.out.print("Seven hundred ");
break;
case 8:
System.out.print("Eight hundred ");
break;
case 9:
System.out.print("Nine hundred ");
}
}
// This If-Else statement will only run if (USER_INPUT % 100) has a value.
if (digitMiddleRight >= 11 && digitMiddleRight <= 19) // If (USER INPUT % 100) has a value between the range 11 to 19.
{
switch (digitMiddleRight) {
case 11:
System.out.print("Eleven ");
break;
case 12:
System.out.print("Twelve ");
break;
case 13:
System.out.print("Thirteen ");
break;
case 14:
System.out.print("Fourteen ");
break;
case 15:
System.out.print("Fifteen ");
break;
case 16:
System.out.print("Sixteen ");
break;
case 17:
System.out.print("Seventeen ");
break;
case 18:
System.out.print("Eighteen ");
break;
case 19:
System.out.print("Nineteen ");
}
} else // if (USER INPUT % 100) is not equal to the values in the range 11-19
{
switch (tens) // The middle digit.
{
case 1:
System.out.print("Ten ");
break;
case 2:
System.out.print("Twenty ");
break;
case 3:
System.out.print("Thirty ");
break;
case 4:
System.out.print("Forty ");
break;
case 5:
System.out.print("Fifty ");
break;
case 6:
System.out.print("Sixty ");
break;
case 7:
System.out.print("Seventy ");
break;
case 8:
System.out.print("Eighty ");
break;
case 9:
System.out.print("Ninety ");
}
switch (ones) // Right most digit.
{
case 1:
System.out.print("One ");
break;
case 2:
System.out.print("Two ");
break;
case 3:
System.out.print("Three ");
break;
case 4:
System.out.print("Four ");
break;
case 5:
System.out.print("Five ");
break;
case 6:
System.out.print("Six ");
break;
case 7:
System.out.print("Seven ");
break;
case 8:
System.out.print("Eight ");
break;
case 9:
System.out.print("Nine "); // Notice how System.out.print() is commonly used. Since this SOP statement prints on the same line and shit.
}
}
} else {
System.out.print("Invalid Value!"); // If the inputted value IS NOT in range -999 to 999.
}
s.close();
}
}