Conver a Number into Words.
Application to print the word for each digit present for a given number.
In this program we have to create two arrays one is ones array with the words of one,two,three.....nineteen.
Then create another array with tens array contains ten,twenty,.....ninety.
package com.javabynataraj;
public class Num2String {
public static void main(String[] args) {
String ones[] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen",};
String tens[] = {"ten ","twenty ","thirty ","fourty ","fifty ","sixty ","seventy ","eighty ","ninety "};
int n = 7677;
int i;
String res=" ";
if(n>1000){
i=n/1000;
res=ones[i-1]+" thousand ";
n=n%1000;
}
if(n>99 && n<1000){
i=n/100;
res=res+ones[i-1]+" hundred ";
n=n%100;
}
if(n>19 && n<100){
i=n/10;
res=res+tens[i-1];
n=n%10;
}
if(n>0 && n<20){
res=res+ones[n-1];
}
System.out.println(res);
}
}
Output: