|
| 1 | +import java.time.LocalDate; |
| 2 | +import java.time.LocalTime; |
| 3 | +import java.util.Scanner; |
| 4 | +public class Verbalize3524{ |
| 5 | + // Arrays to store the verbal representations of units and tens |
| 6 | + private static final String[] units = { |
| 7 | + "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", |
| 8 | + "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" |
| 9 | + }; |
| 10 | + private static final String[] tens = { |
| 11 | + "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" |
| 12 | + }; |
| 13 | + public static void main(String[] args) { |
| 14 | + System.out.println("2021503524 - Mugundh J B"); |
| 15 | + System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now()); |
| 16 | + Scanner scanner = new Scanner(System.in); |
| 17 | + System.out.print("Enter a number between 1 and 9999: "); |
| 18 | + int number = scanner.nextInt(); |
| 19 | + // Check for invalid input |
| 20 | + while (number < 1 || number > 9999) { |
| 21 | + System.out.print("Invalid input. Please enter a number between 1 and 9999: "); |
| 22 | + number = scanner.nextInt(); |
| 23 | + } |
| 24 | + // Convert the number to its verbal representation |
| 25 | + String verbalized = convertToWords(number); |
| 26 | + System.out.printf("Verbalized form:\n" + verbalized); |
| 27 | + } |
| 28 | + // Convert a number to its verbal representation |
| 29 | + public static String convertToWords(int number) { |
| 30 | + if (number < 20) { |
| 31 | + return units[number]; // Directly use the units array |
| 32 | + } else if (number < 100) { |
| 33 | + return tens[number / 10] + " " + units[number % 10]; // Combine tens and units |
| 34 | + } else if (number < 1000) { |
| 35 | + return units[number / 100] + " Hundred and " + convertToWords(number % 100); // Handling hundreds by recursion |
| 36 | + } else { |
| 37 | + return units[number / 1000] + " Thousand and " + convertToWords(number % 1000); // Handling thousands by recursion |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments