----------------------------------------------------------------------------------
Number To Word Java Program
//2--Two
//12--Twelve
//126-- one hundred twenty six
package basic.NumberProgram;
import java.text.NumberFormat;
import java.util.Scanner;
public class NumberToWord {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number (4 digit only)");
int n=sc.nextInt();
// if(n>0 && n<=9999)//999//9999
System.out.println(printWord(n));//-7686
// else
// System.out.println("Bad input");
}
public static String printWord(int n){//2//-45465
String unique[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve",
"Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
String tens[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninty"};
if(n<0)
return "Minus "+ printWord(-n);//-(-34212)//34212
if(n<20)
{return unique[n];}
if(n<100)//n==50==Twenty Three
{
if(n==20||n==30||n==40||n==50||n==60||n==70||n==80||n==90)
return tens[n/10];
else
return tens[n/10] +(n%10!=0?" ":"")+ unique[n%10];
}
if(n<1000)//654==Six hundred Fifty four//900
{
if(n==100||n==200||n==300||n==400||n==500||n==600||n==700||n==800||n==900)
return printWord(n/100) +" Hundred";
else
return printWord(n/100) +" Hundred"+(n%100!=0?" ":"")+printWord(n%100);//54
}
if(n<100000)//2,022 --Two Thousand Twenty two 29,876 Twenty Nine Thousand Eight Hundred Seventy Six
{
if(n==1000||n==2000||n==3000||n==4000||n==5000||n==6000||n==7000||n==8000||n==9000
||n==10000||n==20000||n==30000||n==40000||n==50000||n==60000||n==70000||n==80000||n==90000)
return printWord(n/1000)+ " Thousand";
else
return printWord(n/1000)+ " Thousand"+(n%1000!=0?" ":"")+printWord(n%1000);//876
}
if(n<10000000)//234,567--Two Lakh Thirty Four Thousnad Five Hundred Sixtry Seven
{
if(n==100000||n==200000||n==300000||n==400000||n==500000||n==600000||n==700000||n==800000||n==900000
||n==1000000||n==2000000||n==3000000||n==4000000||n==5000000||n==6000000||n==7000000||n==8000000||n==9000000)
return printWord(n/100000)+ " Lakh";
else
return printWord(n/100000)+ " Lakh"+(n%100000!=0?" ":"")+printWord(n%100000);//876
}
return null;
}
}
No comments:
Post a Comment