Given a amount in INR the task is to convert it into words. Take maximum limit as ₹999999999.
Example: ₹ 210567. The program should generate the word form of the given amount that is Two Lakh Ten Thousand Five Hundred Sixty Seven.
Java
import java.util.*;
public class CurrencyWord
{
int n;static String word;static int pV;
CurrencyWord()//constructor to initialise values
{
n=0;word="";pV=1;
}
public void input()//for taking input from user
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number <=999999999");
n=sc.nextInt();
}
public void feeder()//feeds 'separator' function with a number
{
int c=0;
while(n>0)//separation loop
{
if(c==1)//for separating 100th place digit
{
if(n%10!=0)//for checking whether it is zero or not
{
formWord(n%10,0);
n=n/10;
pV++;
}
else
{
pV++;
n=n/10;
}
}
else//for separating rest numbers in group of two
{
int x=n%10;
int y=n/10;
int z=y%10;
if(x==0&&z==0)//for checking whether it is zero or not
{
pV=pV+2;
}
else
{
separator(n%100);
}
n=n/100;
}
c++;
}
}
public static void separator(int input)//separates the group extracted number from feeder if its >19 11234
{
int c=0;
if(input<10)
{
formWord(input,0);
pV=pV+2;
}
else if(input>10&&input<20)
{
formWord(input%10,1);
pV=pV+2;
}
else
{
while(input>0)//for separating group extracted number if it is >19
{
formWord(input%10,c);
input=input/10;
c=c+2;pV++;
}
}
c=0;
}
public static void formWord(int in,int arNum)//forms the requird word
{
String[][] ar={{"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "},
{"","Eleven ","Tweleve ","Thirteen ","Forteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "},
{"","Ten ","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninenty "}};
String name[]={"","","","Hundred ","Thousand ","","Lakh ","","Crore ",""};
word=ar[arNum][in]+name[pV]+word;
}
public static void main(String args[])//for calling the function accordingly
{
CurrencyWord obj=new CurrencyWord();
obj.input();
obj.feeder();
System.out.println(word);
}
}Java