Java program number to english conversion

Write a program to print amount in English.i want optimum solution

Input : Any number up to 1 million

Output: Pronunciation of the input number in English

Example Inputs and Outputs

Input : 12345
Output : Twelve Thousands Three Hundred And Forty Five

Input: 19201
Output: Nineteen thousand two hundred and one

Input : 10200
Output: Ten Thousand Two Hundred

check out these links:-

  1. http://www.codeproject.com/Tips/538816/Convert-Numbers-to-Words-Neatly

  2. http://www.geeksforgeeks.org/convert-number-to-words/

maybe they may be of some help…:slight_smile:

Hi,I were written this program in company interview.Though it may not ouput correct for long values.but Idea can be conveyed simply.

import java.util.Scanner;
public class NumberToWord {

String gap[] = {"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty","hundread"};
String num[] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen","twenty"};
String Arr[] = {"ten","hundread","thousand","lakh","carore","arab","kharab"};
public static int getCount(int mynum) {
	int count = 0;
	while(mynum > 0) {
		count++;
		mynum /= 10;
	}
	return count;
}
public void solver(int mynum) {
	int count = getCount(mynum);
	String answer = "";
	int multiplier = 1,div = 1,remainder;
	for(int j = 0 ; j <(count - 1) ; j++) {
		multiplier *=10;
	}
	int tmp;
	if(count == 1 || (mynum > 0 && mynum < 20)) {
			answer +=(num[mynum - 1]);
	}
	else if(count == 2) {
		while(div > 0) {
			
			div = (mynum)/ (multiplier);
			remainder = mynum % multiplier;
			
			if(div >  0) {
				answer +=gap[div - 1] ;
				answer+=" ";
			}
			if(remainder > 0) {
				answer +=num[remainder - 1];
			}
			div /= 10;
		}
	}
	else {
		int l_count = count;
		while(multiplier > 0) {
			
			div = (mynum)/ (multiplier);
			remainder = mynum % multiplier;
			
			if(l_count > 4) {
				if(l_count % 2 == 1) {
					answer +=" "+gap[div - 1];
					multiplier = multiplier / 10;
					if(remainder/multiplier > 0)
					answer +=" "+num[ (remainder / multiplier) - 1];
					remainder %= multiplier ;
					l_count -= 1;
					answer +=" "+Arr[l_count - 2];
				}
				else {
					answer +=" "+num[div - 1];
					tmp =l_count  - 1;
					answer +=" "+Arr[tmp - 2];
				}
			}
			else if(l_count > 2) {
				answer +=" "+num[div - 1];
				answer +=" "+Arr[l_count  - 2];
			}
			else if(l_count == 2 && mynum > 20) {
				answer +=" " +gap[div - 1];
			}
			
			else if(l_count == 1 || (mynum >0 && mynum <20)) {
				if(div > 0)
				answer +=" "+num[div - 1];
			}
			mynum = remainder;
			multiplier /= 10;
			l_count--;
		}
	}
	System.out.print("output ="+answer);
}
public static void main(String...s) {
	NumberToWord run = new NumberToWord();
	Scanner myreader = new Scanner(System.in);
	System.out.print("Enter number :");
	int mynum = myreader.nextInt();
	run.solver(mynum);
}

}

1 Like

package test;

import java.util.InputMismatchException;
import java.util.Scanner;

public class NumberToWords {

private static String units[]= {"zero", "One", "Two", "Three", "Four", "Five","Six", "seven", "Eight", "Nine"};

private static String teens[]={"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
    "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

private static String[] tens = {"", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

private static String[] illions = {"", " Thousand " , " Million " , " Billion ", " Trillion ", " Zillion "};
/**
 * @param args
 */
public static void main(String[] args) {
	
	long beginTime = System.currentTimeMillis();
	Scanner sc = new Scanner(System.in);
	try {
		handlerLargeNumber(sc.nextLong());
	} catch(InputMismatchException ex) {
		System.out.println("You have entered invalid number.");
	}
	System.out.println(System.currentTimeMillis() -  beginTime + " milliseconds");
}
/**
 * Converts any large number into triplets and prepare word string from them.
 * @param large number of type long.
 */
private static void handlerLargeNumber(long largeNumber) {
	
	int numberTriplets[][] = new int[5][1];
	int count = -1;
	boolean negativeFlag = false;
	if (largeNumber == 0l) {
		System.out.println("You entered: Zero");
	} if (largeNumber < 0l) {
		negativeFlag = true;
		largeNumber*=-1;
	}
	for(count = 0 ; ; count++) {
		numberTriplets[count][0] = (int)(largeNumber % 1000L);
		if (largeNumber / 1000l == 0) {
			break;
		} else {
			largeNumber/=1000l;
		}
		if (count == illions.length) {
			System.out.println("Out of range.");
			System.exit(1);
		}
	} 
	StringBuilder convertedWord = new StringBuilder();
	StringBuilder tripletWord = new StringBuilder();
	
	for (int i =0 ;i<=count ;i++) {
			tripletWord = handleTriplet(numberTriplets[i][0]);
			if (tripletWord.length() !=0) {
				convertedWord.insert(0, tripletWord.append(illions[i]));
			}
		}
	
	System.out.println("You entered: " + (negativeFlag==true?"Minus ":"") + convertedWord);
}

/**
 * Converts 3 digit number to word.
 * @param triplet
 * @return
 */
private static StringBuilder handleTriplet(int triplet) {
	StringBuilder tripletWord = new StringBuilder();
	if (triplet != 0) {
		
	if(triplet/100 != 0) {
		tripletWord.append(units[triplet/100]).append(" Hundred");
	}
	int tempTens = (triplet%100)/10;
	if (tempTens !=0 && tempTens !=1) {
		tripletWord.append(" " + tens[tempTens]);
	} else if (tempTens == 1) {
		tripletWord.append(" " + teens[triplet%100 - 10]);
		return tripletWord;
	}
	
	if (triplet%10 != 0) {
		tripletWord.append(" "+ units[triplet%10]);
	}
	
	}
	return tripletWord;
}

}