Jan 31

The following example shows how recusion can be used to convert any number from 0 to 99 into words. eg. NumberToWords.numberToWords(34) returns “Thirty Four”.


public class NumberToWords {
	
	private static final String[] ONES = { 
		"Zero", "One", "Two", "Three", "Four", "Five",
		"Six", "Seven", "Eight", "Nine" };
	private static final String[] TEENS = { 
		"Ten", "Eleven", "Twelve", "Thirteen", null, "Fifteen",
		null, null, "Eighteen", null };
	private static final String[] TENS = {
		null, null, "Twenty", "Thirty", "Forty", "Fifty",
		"Sixty", "Seventy", "Eighty", "Ninety" };

	public static String numberToWords(int number) {
		if (number<10) {
			return ONES[number];
		} else if (number<20) {
			int n = number - 10;
			String words = TEENS[n];
			return words==null ? ONES[n]+"teen" : TEENS[n]; 
		} else {
			int n = number % 10;
			return TENS[number/10] + 
				(n==0 ? "" : (" " + numberToWords(n)));
		}
	}

	public static void main(String[] args) {
		for (int i=0; i<100; i++) {
			System.out.println(i+" "+numberToWords(i));
		}
	}
}

written by objects \\ tags: , ,


18 Responses to “How to convert a number into words using Java?”

  1. zohra Huda Says:

    thank you

  2. kevin E. parulan Says:

    tnx!

  3. vikas gupta Says:

    best approach

  4. boggies Says:

    thanks a lot! that give me a lot of ideas

  5. Manoj Jain Says:

    itnis good example of logic and looping

  6. objects Says:

    Thanks all, glad it helped.

  7. mujiba.... Says:

    tnx 4 da code ..im unstress now….

  8. Abhishek Says:

    //The code for conversion of number to word format as per Indian number system through user given input (Keyboard Input) by Abhishek. Email me if you have any problem (abhishek_singh_rajawat@yahoo.co.in).

    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).

  9. Abhishek Says:

    It works up to 999999999 (Ninety Nine Crore Ninety Nine Lacks Ninety nine Thousand nine Hundred Ninety nine) digits.

  10. Harilal Says:

    Thanks a lot… Mr.Abhishek….Great program……..working perfectly………

  11. Franz Says:

    How does this statement work?:

    return words==null ? ONES[n]+”teen” : TEENS[n];

    tnx a lot.. πŸ˜€

  12. Franz Says:

    i do not understand the ‘?’ part,,please help me. πŸ™

  13. objects Says:

    Thats a ternary operator, it’s saying if words is null return ONES[n]+”teen” else return TEENS[n]

  14. Sreejesh Says:

    import java.util.Scanner;

    public class NumToWords {
    public static void main(String[] args) {
    NumToWords anObj = new NumToWords();
    Scanner s = new Scanner(System.in);
    while (true) {
    System.out.println();
    System.out.println(“Enter number less than 100 crore:”);

    Integer aNum = s.nextInt();
    anObj.handleNum(aNum);
    }

    }

    private void handleNum(Integer aInt) {

    if (aInt > 9999999) {
    handleCrore(aInt);
    } else if (aInt > 99999) {
    handleLakh(aInt);
    } else if (aInt > 999)
    handleThousand(aInt);
    else if (aInt > 99)
    handleHundred(aInt);
    else if (aInt > 9)
    handleTwoDigit(aInt);
    else if (aInt > 0)
    handleSingleDigit(aInt);
    else
    System.out.println(” Zero or invalid number “);

    }

    private void handleHundred(Integer aInt) {
    Integer partA = aInt / 100;
    handleSingleDigit(partA);
    if (partA > 0)
    System.out.print(” Hundred “);
    Integer partB = aInt % 100;
    if (partB != 0)
    System.out.print(” and “);
    handleTwoDigit(partB);

    }

    private void handleThousand(Integer aInt) {
    Integer partA = aInt / 1000;
    handleTwoDigit(partA);
    if (partA > 0)
    System.out.print(” Thousand”);
    Integer partB = aInt % 1000;
    handleHundred(partB);

    }

    private void handleLakh(Integer aInt) {
    Integer partA = aInt / 100000;
    handleTwoDigit(partA);
    if (partA > 0)
    System.out.print(” Lakh “);
    Integer partB = aInt % 100000;
    handleThousand(partB);

    }

    private void handleCrore(Integer aInt) {
    // TODO Auto-generated method stub
    Integer partA = aInt / 10000000;
    handleTwoDigit(partA);
    if (partA > 0)
    System.out.print(” Crore “);
    Integer partB = aInt % 10000000;
    handleLakh(partB);

    }

    public void handleTwoDigit(Integer aInt) {
    Integer partA = aInt;
    int numA, numB;
    if (partA 9) & partA < 20) {
    int[] numDgt = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
    String[] numStr1 = { " Ten ", " Eleven ", " Twelve ", " Thirteen ",
    " Fourteen ", " Fifteen ", "Sixteen", " Seventeen ",
    " Eighteen ", " Nineteen " };
    for (int i : numDgt) {
    if (partA == i) {
    System.out.print(numStr1[i – 10]);

    }
    }

    } else {
    numA = aInt / 10;
    int[] numDgt = { 2, 3, 4, 5, 6, 7, 8, 9 };
    String[] numStr2 = { " Twenty ", " Thirty ", " Fourty ", " Fifty ",
    " Sixty ", " Seventy ", "Eighty", " Ninety " };

    for (int i : numDgt) {
    if (numA == i)
    System.out.print(numStr2[i – 2]);

    }

    numB = aInt % 10;
    handleSingleDigit(numB);
    }
    }

    public void handleSingleDigit(Integer aInt) {
    int single = aInt;
    int[] numDgt = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    String[] numStr3 = { " One ", " Two ", " Three ", " Four ", " Five ",
    " Six ", " Seven ", " Eight ", " Nine " };

    for (int i : numDgt) {
    if (single == i)
    System.out.print(numStr3[i – 1]);

    }

    }
    }

  15. Prajakta Says:

    Hi,
    I am getting compilation error in this line
    if (partA 9) & partA < 20),Will you please let me know it's solution ?

    Regards,
    Prajakta

  16. objects Says:

    Looks like it should be:

    if (partA > 9 && partA < 20) {

  17. faith Says:

    how about converting 999 into words?
    pls help me.thank you!!!

  18. Sreejesh Says:

    Forgot. Might be if(partA >9 & partA < 20)

Leave a Reply

You must be logged in to post a comment.