-
Notifications
You must be signed in to change notification settings - Fork 14
/
Verbalize3524.java
40 lines (40 loc) · 1.97 KB
/
Verbalize3524.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
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Scanner;
public class Verbalize3524{
// Arrays to store the verbal representations of units and tens
private static final String[] units = {
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
};
private static final String[] tens = {
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
public static void main(String[] args) {
System.out.println("2021503524 - Mugundh J B");
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number between 1 and 9999: ");
int number = scanner.nextInt();
// Check for invalid input
while (number < 1 || number > 9999) {
System.out.print("Invalid input. Please enter a number between 1 and 9999: ");
number = scanner.nextInt();
}
// Convert the number to its verbal representation
String verbalized = convertToWords(number);
System.out.printf("Verbalized form:\n" + verbalized);
}
// Convert a number to its verbal representation
public static String convertToWords(int number) {
if (number < 20) {
return units[number]; // Directly use the units array
} else if (number < 100) {
return tens[number / 10] + " " + units[number % 10]; // Combine tens and units
} else if (number < 1000) {
return units[number / 100] + " Hundred and " + convertToWords(number % 100); // Handling hundreds by recursion
} else {
return units[number / 1000] + " Thousand and " + convertToWords(number % 1000); // Handling thousands by recursion
}
}
}