It can be done by many no. of ways. and the actual concept is firstly store all the low digit numbers upto nineteen like one, two, three, ....... nine, ten, eleven, ..... nineteen in an array. and reset numbers sounds like the combination of 10th position number and one to nine number like(twenty one, thirty two, etc). So now we just need to store all numbers in multiply of 10 after nineteen like(twenty, thirty.... ninety).
Following is the one way to convert the number into words using above concept.
NumberToWord.java:
---------------------------------------
import java.util.*;
public class NumberToWord
{
public void putWord(int n,String ch)
{
String one[]={
" "," one"," two"," three"," four"," five"," six"," seven"," eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
String ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};
if(n>19) { System.out.print(ten[n/10]+" "+one[n%10]);} else { System.out.print(one[n]);}if(n>0)System.out.print(ch);
}
public static void main(String[] args)
{
int n=0;
Scanner s =new Scanner(System.in);
System.out.print("Enter an integer number: ");
n = s.nextInt();
if(n<=0)
System.out.print("Enter numbers greater than 0");
else
{
NumberToWord a =new NumberToWord();
System.out.print("After conversion number in words is :");
a.putWord((n/1000000000)," Hundred");
a.putWord((n/10000000)%100," crore");
a.putWord(((n/100000)%100)," lakh");
a.putWord(((n/1000)%100)," thousand");
a.putWord(((n/100)%10)," hundred");
a.putWord((n%100)," ");
}
}
}
/******************************* Finish Source Code *******************************/
5