Skip to content

Instantly share code, notes, and snippets.

@adityachaudhari
Last active June 27, 2017 20:14
Show Gist options
  • Save adityachaudhari/18ad4821befa6ceea0c1ef3b06e4ce3c to your computer and use it in GitHub Desktop.
Save adityachaudhari/18ad4821befa6ceea0c1ef3b06e4ce3c to your computer and use it in GitHub Desktop.
Write a program to print numeric number into English words. (Minimum 3 digit numeric) Input: 947 Output: nine hundred forty seven
package com.examples.java8.ListTomap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by adityacchaudhari on 27/06/2017.
*/
public class NumberToWords {
//Declare map
static Map<Integer, String> zeroToTwentyNumToWordsMap = new HashMap<>();
static Map<Integer, String> tensWordsMap = new HashMap<>();
public static void main(String args[]) throws IOException{
//load initial values
loadInitialMapValues();
int numberToConvertToWord=120;
String result = getWordFromNumber(numberToConvertToWord);
System.out.println("Input : " +numberToConvertToWord+ " \nOutput : "+ result );
}
public static String getWordFromNumber(int no) throws ArithmeticException {
if (no <= 20) {
return zeroToTwentyNumToWordsMap.get(no);
}
else if(no >20 && no<=99){
return twoDigitNumberToWord(no);
}else if (no >=100 && no <= 999) {
return threeDigitNumberToWord(no);
} else {
System.out.println("INPUT NO IS OUT OF SCOPE REQUEST YOU TO PLEASE ENTER THREE DIGIT NUMBER THANKS .");
}
return "UNKNOWN INPUT";
}
private static String twoDigitNumberToWord(int no) {
String firstDigit= tensWordsMap.get(no/10);
String secondDigit=zeroToTwentyNumToWordsMap.get(no%10);
if(secondDigit.equals("zero")){
secondDigit ="";
}
return firstDigit+" "+secondDigit;
}
private static String threeDigitNumberToWord(int no) {
String firstDigit= zeroToTwentyNumToWordsMap.get(no/100);
String secondDigit=twoDigitNumberToWord(no%100);
if(no==100){
return "hundred";
}
return firstDigit+" "+secondDigit;
}
private static void loadInitialMapValues() {
//initialize 0 to 20 values
zeroToTwentyNumToWordsMap.put(0, "zero");
zeroToTwentyNumToWordsMap.put(1, "one");
zeroToTwentyNumToWordsMap.put(2, "two");
zeroToTwentyNumToWordsMap.put(3, "three");
zeroToTwentyNumToWordsMap.put(4, "four");
zeroToTwentyNumToWordsMap.put(5, "five");
zeroToTwentyNumToWordsMap.put(6, "six");
zeroToTwentyNumToWordsMap.put(7, "seven");
zeroToTwentyNumToWordsMap.put(8, "eight");
zeroToTwentyNumToWordsMap.put(9, "nine");
zeroToTwentyNumToWordsMap.put(10, "ten");
zeroToTwentyNumToWordsMap.put(11, "eleven");
zeroToTwentyNumToWordsMap.put(12, "twelve");
zeroToTwentyNumToWordsMap.put(13, "thirteen");
zeroToTwentyNumToWordsMap.put(14, "fourteen");
zeroToTwentyNumToWordsMap.put(15, "fifteenth");
zeroToTwentyNumToWordsMap.put(16, "sixteen");
zeroToTwentyNumToWordsMap.put(17, "seventeen");
zeroToTwentyNumToWordsMap.put(18, "eighteen");
zeroToTwentyNumToWordsMap.put(19, "nineteen");
zeroToTwentyNumToWordsMap.put(20, "twenty");
//initialize tens values
tensWordsMap.put(0, "");
tensWordsMap.put(1, "");
tensWordsMap.put(2, "twenty");
tensWordsMap.put(3, "thirty");
tensWordsMap.put(4, "forty");
tensWordsMap.put(5, "fifty");
tensWordsMap.put(6, "sixty");
tensWordsMap.put(7, "seventy");
tensWordsMap.put(8, "eighty");
tensWordsMap.put(9, "ninety");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment