Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created February 12, 2022 07:41
Show Gist options
  • Select an option

  • Save aadipoddar/67065de91380c1402facfbb8dc84617b to your computer and use it in GitHub Desktop.

Select an option

Save aadipoddar/67065de91380c1402facfbb8dc84617b to your computer and use it in GitHub Desktop.
Convert the Given Number to Word Form
/*
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