Sunday, September 3, 2017

Number To Equivalent InWords String

Code:
package com.nearur;

import java.util.Scanner;

public class English {

String[] ones= {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
String[] tens= {"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
StringBuffer buffer=new StringBuffer();

String c(int n) {
if(n<0) {
return "Minus "+c(-n);
}else if(n<20) {
return ones[n];
}else if(n<100) {
return tens[n/10]+" "+ones[n%10];
}else if(n<1000) {
return c(n/100)+" hundred "+c(n%100);
}else if(n<100000) {
return c(n/1000)+" thousand "+c(n%1000);
}else if(n<10000000) {
return c(n/100000)+" lakh "+c(n%100000);
}else if(n<1000000000) {
return c(n/10000000)+" crore "+c(n%10000000);
}

return "";
}

public static void main(String[] args) {
English a=new English();
Scanner in =new Scanner(System.in);
System.out.print("Enter Any Number : ");
int n=in.nextInt();
System.out.println(a.c(n).toUpperCase());

}

}

Output: