Created
February 12, 2022 07:41
-
-
Save aadipoddar/67065de91380c1402facfbb8dc84617b to your computer and use it in GitHub Desktop.
Convert the Given Number to Word Form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Accept a number less than 100 and convert it into word form | |
| Eg: | |
| input: 45 | |
| output: forty-five | |
| input: 100 | |
| output: Out of Range! | |
| */ | |
| import java.util.Scanner; | |
| class NumberToWord { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| System.out.println("Enter a number less than 100: "); | |
| int number = sc.nextInt(); | |
| if (number >= 100 || number < 0) { | |
| System.out.println("Out of Range!"); | |
| return; | |
| } | |
| String[] units = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; | |
| String[] teens = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", | |
| "eighteen", "ninteen" }; | |
| String[] tens = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" }; | |
| String word = ""; | |
| if (number >= 0 && number < 10) | |
| word = units[number]; | |
| else if (number >= 10 && number < 20) | |
| word = teens[number - 10]; | |
| else if (number >= 20 && number < 100) { | |
| int tensDigit = number / 10; | |
| int unitsDigit = number % 10; | |
| word = tens[tensDigit - 2]; | |
| if (unitsDigit > 0) | |
| word += "-" + units[unitsDigit]; | |
| } | |
| System.out.println(word); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment