In this post we will convert an integer into words in the Indian Numbering System. In Indian Numbering System numbers over 9,999 are written in two-digit groups rather than the three-digit groups used in most other parts of the world. The terms *lakh*, *crore*, *arab* etc are used to represent large numbers. A similar problem once came in ISC Practical of 2000 but there one was suppose to convert integers up to 9999 but here will be doing the same for the entire range of positive integers i.e. up to 2147483647.

There is no pattern for numbers up to nineteen—1 is One, 2 is Two, 18 is Eighteen and so on, therefore we will take a String array arr1 to store numbers from One through Nineteen. From 20 there is a pattern—21 is Twenty One, 22 is Twenty Two, 31 is Thirty One, 32 is Thiry Two and so on. We will take another String array arr2 to store “Twenty”, “Thirty”, “Forty”… etc. We will use arr1 for numbers less than 20 and we will use both arr1 and arr2 for numbers more than 20. For example if the number is 18 we will use arr1[18-1] to get the string equivalent, we are subtracting 1 because in Java array elements begin from 0. If the number is 45 we will split the number into ones part and tens part and use arr2[4-2]+” “+arr2[2-1] to get the string equivalent. We are subtracting 2 in this case because in arr2 “Twenty” is stored at location 0.

We will be adding “Hundred”, “Thousand”… etc for numbers more than 100 and we will take an integer array for storing the factors in reverse order i.e. we will first divide by 10000000 (10^9) to find the number of Arabs, then by 10000000 to find number of Crores and so.

After incorporating all the concepts discussed above we get the following program:

class NumberSystem{ public static String convertIntegerToWords(int n){ if(n==0) return "Zero"; String arr1[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; String arr2[]={"Twenty","Thirty", "Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; String unit[]={"Arab","Crore","Lakh","Thousand","Hundred", ""}; int factor[]={1000000000, 10000000, 100000, 1000, 100,1}; String answer=""; if(n<0){ answer="Negative"; n=-n; } int quotient, units, tens; for(int i=0; i0){ if(quotient<20){ answer = answer + " " + arr1[quotient-1]; }else{ units=quotient%10; tens=quotient/10; if(units!=0 ) answer = answer + " " + arr2[tens-2] + " " + arr1[units-1]; else answer = answer + " " + arr2[tens-2] + " " ; } answer = answer + " " + unit[i]; } n=n%factor[i]; } return answer.trim(); } public static void main(String args[]){ System.out.println("2147483647: "+convertIntegerToWords(2147483647)); System.out.println("10005006: "+convertIntegerToWords(10005006)); System.out.println("-10005006: "+convertIntegerToWords(-10005006)); } } }

The main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.