# Converting a number into its equalant value in words in Java

By: Ganesh Iyer Emailed: 1729 times Printed: 2337 times

 By: rohit kumar - how this program is work By: Kirti - Hi..thx for the hadoop in By: Spijker - I have altered the code a By: ali mohammed - why we use the java in ne By: ali mohammed - why we use the java in ne By: mizhelle - when I exported the data By: raul - no output as well, i'm ge By: Rajesh - thanx very much... By: Suindu De - Suppose we are executing

In this tutorial, i will explain how a number can be converted to its value as a word using Java. If you are wondering how and where this can be used, take the example of Money values. If the Amount is in numbers and you would like to convert the amount in words then this program will be useful for you.

/* Author         - Ganesh Iyer
Program        - Displaying the input number into words
Written on     - 06-06-2009 */

``` class constNumtoLetter   {       String[] unitdo ={"", " One", " Two", " Three", " Four", " Five",          " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve",          " Thirteen", " Fourteen", " Fifteen",  " Sixteen", " Seventeen",           " Eighteen", " Nineteen"};       String[] tens =  {"", "Ten", " Twenty", " Thirty", " Forty", " Fifty",          " Sixty", " Seventy", " Eighty"," Ninety"};       String[] digit = {"", " Hundred", " Thousand", " Lakh", " Crore"};      int r;       //Count the number of digits in the input number       int numberCount(int num)       {           int cnt=0;           while (num>0)           {             r = num%10;             cnt++;             num = num / 10;           }             return cnt;       }       //Function for Conversion of two digit       String twonum(int numq)       {            int numr, nq;            String ltr=\"";            nq = numq / 10;            numr = numq % 10;            if (numq>19)              {            ltr=ltr+tens[nq]+unitdo[numr];              }            else              {            ltr = ltr+unitdo[numq];              }            return ltr;       }       //Function for Conversion of three digit       String threenum(int numq)       {              int numr, nq;              String ltr = "";              nq = numq / 100;              numr = numq % 100;              if (numr == 0)               {               ltr = ltr + unitdo[nq]+digit[1];                }              else               {               ltr = ltr +unitdo[nq]+digit[1]+" and"+twonum(numr);               }              return ltr;       } }  class originalNumToLetter    {       public static void main(String[] args) throws Exception       {           //Defining variables q is quotient, r is remainder           int len, q=0, r=0;           String ltr = " ";           String Str = "Rupees";           constNumtoLetter n = new constNumtoLetter();           int num = Integer.parseInt(args[0]);           if (num <= 0) System.out.println(\"Zero or Negative number not for conversion");           while (num>0)           {              len = n.numberCount(num);              //Take the length of the number and do letter conversion              switch (len)              {                   case 8:                           q=num/10000000;                           r=num%10000000;                           ltr = n.twonum(q);                           Str = Str+ltr+n.digit[4];                           num = r;                           break;                   case 7:                   case 6:                           q=num/100000;                           r=num%100000;                           ltr = n.twonum(q);                           Str = Str+ltr+n.digit[3];                           num = r;                           break;                   case 5:                   case 4:                            q=num/1000;                            r=num%1000;                            ltr = n.twonum(q);                            Str= Str+ltr+n.digit[2];                            num = r;                            break;                   case 3:                             if (len == 3)                                 r = num;                             ltr = n.threenum(r);                             Str = Str + ltr;                             num = 0;                             break;                   case 2:                            ltr = n.twonum(num);                            Str = Str + ltr;                            num=0;                            break;                   case 1:                            Str = Str + n.unitdo[num];                            num=0;                            break;                   default:                           num=0;                           System.out.println(\"Exceeding Crore....No conversion");                           System.exit(1);               }                           if (num==0)                           System.out.println(Str+\" Only");             }          }       } ```
To Execute

java originalNumToLetter <INPUT>

Ex -   java originalNumToLetter 1234

Output

Rupees One Thousand Two Hundred and Thirty Four Only

 1 View Comment`Thanks and easy to understand.....impressive logic...` View Tutorial          By: Dj at 2009-11-26 13:33:34 2 View Comment`Thanks alot.. it helped me a lot...` View Tutorial          By: Sushmitha Rao at 2010-06-02 02:39:51 3 View Comment```really helpul........ pls mail me some more logical programs of java thnxx a lot``` View Tutorial          By: shivani at 2010-07-17 01:47:30 4 View Comment```//Convert numbers to word as per Indian number system in java by if loop. It works up to 999999999. import java.util.*; public class NumtoWord { public static void main(String[] args) { String a; Scanner s = new Scanner(System.in); System.out.print("Enter a Number : "); a = s.next(); int b = a.length(); String str[] = {"1","One","2","Two","3","Three","4","Four","5","Five","6","Six","7","Seven","8","Eight","9","Nine","10","Ten","11","Eleven","12","Twelve","13","Thirteen","14","Forteen","15","Fifteen","16","Sixteen","17","Seventeen","18","Eighteen","19","Nineteen","20","Twenty","30","Thirty","40","Fourty","50","Fifty","60","Sixty","70","Seventy","80","Eighty","90","Ninty","100","Hundred"}; System.out.println(""); if (b==9) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s4= a.substring(3,4); String s5= a.substring(4,5); String s6= a.substring(5,6); String s7= a.substring(6,7); String s8= a.substring(7,8); String s9= a.substring(8,9); String s10= a.substring(0,2); String s11= a.substring(2,4); String s12= a.substring(4,6); String s14= a.substring(7,9); { if (s10.equals("00")) System.out.print(""); else if (s1.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print("\n" + str[r+1] + " Crore "); } else { { for (int i=0;i<=40;i++) if (str[i].equals(s1)) System.out.print("\n" + str[i+37] + " "); } { if(s2.equals("0")) { System.out.print("Crore "); } else for (int j=0;j<=40;j++) { if (str[j].equals(s2)) System.out.print(str[j+1] + " Crore "); } } } } { if (s11.equals("00")) System.out.print(""); else if (s3.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s11)) System.out.print(str[r+1] + " Lacks "); } else { { for (int k=0;k<=38;k++) if (str[k].equals(s3)) System.out.print(str[k+37] + " "); } { if(s4.equals("0")) { System.out.print("Lacks "); } else for (int l=0;l<=38;l++) { if (str[l].equals(s4)) System.out.print(str[l+1] + " Lacks "); } } } } { if (s12.equals("00")) System.out.print(""); else if (s5.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s12)) System.out.print(str[r+1] + " Thousand "); } else { { for (int m=0;m<=38;m++) if (str[m].equals(s5)) System.out.print(str[m+37] + " "); } { if(s6.equals("0")) { System.out.print("Thousand "); } else for (int n=0;n<=38;n++) { if (str[n].equals(s6)) System.out.print(str[n+1] + " Thousand "); } } } } { for (int o=0;o<=40;o++) if (str[o].equals(s7)) System.out.print(str[o+1] + " Hundred "); } { if (s14.equals("00")) System.out.print(""); else if (s8.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s14)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s8)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s9)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==8) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s4= a.substring(3,4); String s5= a.substring(4,5); String s6= a.substring(5,6); String s7= a.substring(6,7); String s8= a.substring(7,8); String s10= a.substring(1,3); String s11= a.substring(3,5); String s12= a.substring(6,8); { if (s1.equals("0")) System.out.print(""); else for (int i=0;i<=40;i++) if (str[i].equals(s1)) System.out.print("\n" + str[i+1] + " Crore "); } { if (s10.equals("00")) System.out.print(""); else if (s2.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1] + " Lacks "); } else { { for (int k=0;k<=38;k++) if (str[k].equals(s2)) System.out.print(str[k+37] + " "); } { if(s3.equals("0")) { System.out.print("Lacks "); } else for (int l=0;l<=38;l++) { if (str[l].equals(s3)) System.out.print(str[l+1] + " Lacks "); } } } } { if (s11.equals("00")) System.out.print(""); else if (s4.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s11)) System.out.print(str[r+1] + " Thousand "); } else { { for (int m=0;m<=38;m++) if (str[m].equals(s4)) System.out.print(str[m+37] + " "); } { if(s5.equals("0")) { System.out.print("Thousand "); } else for (int n=0;n<=38;n++) { if (str[n].equals(s5)) System.out.print(str[n+1] + " Thousand "); } } } } { for (int o=0;o<=40;o++) if (str[o].equals(s6)) System.out.print(str[o+1] + " Hundred "); } { if (s12.equals("00")) System.out.print(""); else if (s7.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s12)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s7)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s8)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==7) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s4= a.substring(3,4); String s5= a.substring(4,5); String s6= a.substring(5,6); String s7= a.substring(6,7); String s10= a.substring(0,2); String s11= a.substring(2,4); String s12= a.substring(5,7); { if (s10.equals("00")) System.out.print(""); else if (s1.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1] + " Lacks "); } else { { for (int k=0;k<=38;k++) if (str[k].equals(s1)) System.out.print(str[k+37] + " "); } { if(s2.equals("0")) { System.out.print("Lacks "); } else for (int l=0;l<=38;l++) { if (str[l].equals(s2)) System.out.print(str[l+1] + " Lacks "); } } } } { if (s11.equals("00")) System.out.print(""); else if (s3.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s11)) System.out.print(str[r+1] + " Thousand "); } else { { for (int m=0;m<=38;m++) if (str[m].equals(s3)) System.out.print(str[m+37] + " "); } { if(s4.equals("0")) { System.out.print("Thousand "); } else for (int n=0;n<=38;n++) { if (str[n].equals(s4)) System.out.print(str[n+1] + " Thousand "); } } } } { for (int o=0;o<=40;o++) if (str[o].equals(s5)) System.out.print(str[o+1] + " Hundred "); } { if (s12.equals("00")) System.out.print(""); else if (s6.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s12)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s6)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s7)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==6) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s4= a.substring(3,4); String s5= a.substring(4,5); String s6= a.substring(5,6); String s10= a.substring(1,3); String s11= a.substring(4,6); { if(s1.equals("0")) System.out.print(""); else { for (int j=0;j<=40;j++) if (str[j].equals(s1)) System.out.print(str[j+1] + " Lacks "); } } { if (s10.equals("00")) System.out.print(""); else if (s2.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1] + " Thousand "); } else { { for (int m=0;m<=40;m++) if (str[m].equals(s2)) System.out.print(str[m+37] + " "); } { if(s3.equals("0")) { System.out.print("Thousand "); } else for (int n=0;n<=38;n++) { if (str[n].equals(s3)) System.out.print(str[n+1] + " Thousand "); } } } } { if(s4.equals("0")) System.out.print(""); else { for (int o=0;o<=40;o++) if (str[o].equals(s4)) System.out.print(str[o+1] + " Hundred "); } } { if (s11.equals("00")) System.out.print(""); else if (s5.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s11)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s5)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s6)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==5) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s4= a.substring(3,4); String s5= a.substring(4,5); String s10= a.substring(0,2); String s11= a.substring(3,5); { if (s10.equals("00")) System.out.print(""); else if (s1.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1] + " Thousand "); } else { { for (int m=0;m<=38;m++) if (str[m].equals(s1)) System.out.print(str[m+37] + " "); } { if(s2.equals("0")) { System.out.print("Thousand "); } else for (int n=0;n<=38;n++) { if (str[n].equals(s2)) System.out.print(str[n+1] + " Thousand "); } } } } { for (int o=0;o<=40;o++) if (str[o].equals(s3)) System.out.print(str[o+1] + " Hundred "); } { if (s11.equals("00")) System.out.print(""); else if (s4.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s11)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s4)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s5)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==4) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s4= a.substring(3,4); String s10= a.substring(2,4); { if(s1.equals("0")) System.out.print(""); else { for (int j=0;j<=40;j++) if (str[j].equals(s1)) System.out.print(str[j+1] + " Thousand "); } } { if(s2.equals("0")) System.out.print(""); else { for (int o=0;o<=40;o++) if (str[o].equals(s2)) System.out.print(str[o+1] + " Hundred "); } } { if (s10.equals("00")) System.out.print(""); else if (s3.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s3)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s4)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==3) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s3= a.substring(2,3); String s10= a.substring(1,3); { if(s1.equals("0")) System.out.print(""); else { for (int o=0;o<=40;o++) if (str[o].equals(s1)) System.out.print(str[o+1] + " Hundred "); } } { if (s10.equals("00")) System.out.print(""); else if (s2.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s2)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s3)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==2) { String s1= a.substring(0,1); String s2= a.substring(1,2); String s10= a.substring(0,2); { if (s10.equals("00")) System.out.print(""); else if (s1.equals("1")) { for (int r=0;r<=40;r++) if (str[r].equals(s10)) System.out.print(str[r+1]); System.out.print("\n"); } else { for (int p=0;p<=40;p++) if (str[p].equals(s1)) System.out.print(str[p+37]); for (int q=0;q<=40;q++) { if (str[q].equals(s2)) System.out.print(" " + str[q+1]); } } System.out.print("\n"); } } else if (b==1) { String s1= a.substring(0,1); for (int q=0;q<=40;q++) if (str[q].equals(s1)) System.out.print(" " + str[q+1]); } System.out.println("\n"); } } // By Abhishek Singh (abhishek_singh_rajawat@yahoo.co.in).``` View Tutorial          By: Abhishek at 2011-09-05 10:42:20 5 View Comment`Thanks a ton Ganesh...` View Tutorial          By: Anonymous at 2012-01-25 11:20:40 6 View Comment```What is the \ in several of the string building statements. i.e. System.out.println(Str+\" Only");``` View Tutorial          By: rogerb at 2012-01-28 23:07:31 7 View Comment```Thanks a lot for valuable program. But I am unable to use it in JSP..i mean how to pass parameter through jsp. as we can use java program in jsp but how to pass parameters to above program?? I am unable to pass. Please help someone..!! Thanks in advance.``` View Tutorial          By: Kailash at 2012-07-30 10:09:12 8 View Comment```really helpul........ pls mail me some more logical programs of java``` View Tutorial          By: senthil at 2013-08-19 17:53:02

Your email(required, will not be shown to the public):

More Tutorials by Ganesh Iyer
 Converting a number into its equalant value in words in Java Method Overloading sample in Java Method Override sample in Java A Java program which takes contents of one file and sends the output to another file by adding line numbers to each line. Taking the size of an Array at runtime & generate random numbers to populate the Array How to reverse the elements in a Single dimension array and display the output How to count the number of digits in any Input number other than negative numbers

More Tutorials in Java
 Update contents of a file within a jar file Tomcat and httpd configured in port 8080 and 80 Java File Java String Count number of vowels, consonants and digits in a String in Java Reverse a number in Java Student marks calculation program in Java Handling Fractions in Java Calculate gross salary in Java Calculate average sale of the week in Java Vector in Java - Sample Program MultiLevel Inheritance sample in Java Multiple Inheritance sample in Java Java program using Method Overriding Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
 How to Send SMS using Java Program (full code sample included) How to use ArrayList in Java Method Overloading (function overloading) in Java Method Overriding in Java wait(), notify() and notifyAll() in Java - A tutorial What is Java? XML and Java - Parsing XML using Java Tutorial FileReader and FileWriter example program in Java Count number of vowels, consonants and digits in a String in Java The Basic Structure of a Simple Java program arraycopy example in Java Recursion in java Stack example in Java - push(), pop(), empty(), search() Calendar - sample program in Java Execute system commands in a Java Program
Most Emailed Articles (in Java)
 Vector in Java - Sample Program instanceof operator example in Java 3-D Secure Payment or 3DS - (3-Domain Secure Payment) What is a Java Package and how to use it? Use of return statement in Java How to use Iterator in Java J2EE Components The java Buzzwords The Basic Structure of a Simple Java program Java Chat Client Sample Applet MultiLevel Inheritance sample in Java What is JasperReports? java.lang.reflect package Applet Notinited : Applet xxxxxxxxxxxx notinited The if-then Statement in Java