Java 数字转换

Java 数字转换,java,jgrasp,Java,Jgrasp,我想获得一些关于将整数转换为word的帮助。我对Java非常陌生,我对自己将要做的事情有一些想法,但需要一些帮助 代码应打印0-999之间的数字字,一旦在扫描仪中键入-1,程序应停止 像这样: 请键入介于0和999之间的数字:1 一个 请键入介于0和999之间的数字:11 十一 请键入介于0和999之间的数字:122 一百二十二 请键入一个介于0和999:1000之间的数字 数字超出范围 请键入介于0和999之间的数字:-1 感谢您使用我们的程序 我还必须使用一些方法使这个“更干净”首先通过10

我想获得一些关于将整数转换为word的帮助。我对Java非常陌生,我对自己将要做的事情有一些想法,但需要一些帮助

代码应打印0-999之间的数字字,一旦在扫描仪中键入-1,程序应停止

像这样:

请键入介于0和999之间的数字:1

一个

请键入介于0和999之间的数字:11

十一

请键入介于0和999之间的数字:122

一百二十二

请键入一个介于0和999:1000之间的数字

数字超出范围

请键入介于0和999之间的数字:-1

感谢您使用我们的程序


我还必须使用一些方法使这个“更干净”

首先通过100的除法取百位数字,然后通过调用方法打印相应的数字
numberToWord((number/100),“100”),因为数字/100将在0到9之间,所以它将打印由100连接的字中的数字

现在您留下了两位数的号码,您可以直接调用
numberToWord((号码%100),“”)因为我们取的是数字的模100,所以它只能传递两位数
如果(num>19){System.out.print(十个[num/10]+“”+one[num%10]);}
则它将产生十个字,并以一个字串联打印十个字
else{System.out.print(one[num]);}
直接从数组中打印1到19之间的单词

import java.util.Scanner;

public class Test1 {



        public static void main(String[] args) {
            int number = 0;
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
            number = scanner.nextInt();
            while(number!=-1){
                if(number>=0 && number<=999){
                    if(number==0){
                        System.out.print("NUMBER AFTER CONVERSION:\tZERO");
                    } else {
                        System.out.print("NUMBER AFTER CONVERSION:\t");
                        numberToWord(((number / 100) % 10), " HUNDRED");
                        numberToWord((number % 100), " ");
                    }

                } else{
                    System.out.print("NUMBER OUT OF RANGE");
                }
                System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit:  ");
                number = scanner.nextInt();
            }
        }

        public static void numberToWord(int num, String val) {
            String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"
            };
            String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
            if (num > 19) {
                System.out.print(tens[num / 10] + " " + ones[num % 10]);
            } else {
                System.out.print(ones[num]);
            }
            if (num > 0) {
                System.out.print(val);
            }
        }
    }

这是数字转换程序的递归解决方案

 string unitsMap[] = { "zero", "one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tensMap[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

string NumberToWords(int number)
{
 if (number == 0)
    return "zero";

if (number < 0)
    return "minus " + NumberToWords(abs(number));

string words = "";

if ((number / 1000000000) > 0)
{
    words += NumberToWords(number / 1000000000) + " billion ";
    number %= 1000000000;
}

if ((number / 1000000) > 0)
{
    words += NumberToWords(number / 1000000) + " million ";
    number %= 1000000;
}

if ((number / 1000) > 0)
{
    words += NumberToWords(number / 1000) + " thousand ";
    number %= 1000;
}

if ((number / 100) > 0)
{
    words += NumberToWords(number / 100) + " hundred ";
    number %= 100;
}

if (number > 0)
{
    if (number < 20)
        words += unitsMap[number];
    else
    {
        words += tensMap[number / 10];
        if ((number % 10) > 0)
            words += "-" + unitsMap[number % 10];
    }
}

return words;
 }
字符串单位映射[]={“零”、“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”、“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”、“十九”};
字符串时态图[]={“零”、“十”、“二十”、“三十”、“四十”、“五十”、“六十”、“七十”、“八十”、“九十”};
字符串数字字(整数)
{
如果(数字==0)
返回“零”;
如果(数字<0)
返回“减号”+NumberToWords(abs(number));
字串=”;
如果((数量/100000000)>0)
{
单词+=数字单词(数字/1000000000)+“十亿”;
数字%=100000000;
}
如果((数量/1000000)>0)
{
单词+=数字单词(数字/1000000)+“百万”;
数字%=1000000;
}
如果((数字/1000)>0)
{
单词+=数字单词(数字/1000)+“千”;
数字%=1000;
}
如果((数字/100)>0)
{
单词+=数字单词(数字/100)+“百”;
数字%=100;
}
如果(数字>0)
{
如果(数字<20)
单词+=单位映射[数字];
其他的
{
单词+=时态图[数字/10];
如果((数字%10)>0)
单词+=“-”+单位映射[数字%10];
}
}
返回单词;
}

以下是您的操作方法。我把它从1扩展到10,但是你可以把它从1扩展到999

   //     values entered        |     result
   //     2                     |     You have entered:Two
   //     10                    |     You have entered: Ten
   //     23                    |     You have entered: 23 which is not in range.

int i = Integer.parseInt(txtInput.getText());
    String[] namesOfNums = {"One", "Two", "Three","Four", "Five", "Six", "Seven","Eight", "Nine","Ten" };
    String output = "";
    if (i > 0 && i <=10) 
    {

        output = namesOfNums[i - 1];
        lblDisplay.setText("You have entered: "+ output);

    }
    else
    {
      lblDisplay.setText("You have entered: "+ i +" which is not range.");
    }
//输入的值|结果
//2 |您已输入:两个
//10 |您已输入:10
//23 |您已输入:23,不在范围内。
int i=Integer.parseInt(txtInput.getText());
字符串[]nameofnums={“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”};
字符串输出=”;
如果(i>0&&i
public class NumberToWord
{
私有静态最终字符串[]特殊名称={
"",
“千”,
“百万”,
“十亿”,
“万亿”,
“万亿美元”,
“五百万”
};
私有静态最终字符串[]tensNames={
"",
“十”,
“二十”,
“三十”,
“四十”,
“五十”,
“六十”,
“七十”,
“八十”,
“九十”
};
私有静态最终字符串[]numNames={
"",
“一个”,
“两个”,
“三”,
“四”,
“五个”,
“六”,
“七”,
“八”,
“九”,
“十”,
“十一”,
“十二”,
“十三”,
“十四”,
“十五”,
“十六”,
“十七”,
“十八”,
“十九”
};
私有字符串转换器sthanonethousand(整数){
串电流;
如果(数量%100<20){
当前=numNames[数量%100];
数目/=100;
}
否则{
当前=numNames[编号%10];
数目/=10;
当前=当前名称[编号%10]+当前;
数目/=10;
}
如果(数字==0)返回电流;
返回numNames[数字]+“百”+当前值;
}
公共字符串转换(整数){
如果(number==0){返回“零”;}
字符串前缀=”;
如果(数字<0){
数字=-数字;
前缀=“负”;
}
字符串current=“”;
int place=0;
做{
int n=数字%1000;
如果(n!=0){
字符串s=转换器的内部和(n);
电流=s+特殊名称[位置]+电流;
}
place++;
数目/=1000;
}而(数量>0);
返回(前缀+当前).trim();
}
公共静态void main(字符串[]args){
NumberToWord obj=新的NumberToWord();
System.out.println(“***”+obj.convert(123456789));
System.out.println(“***”+obj.convert(-55));
}
}
来源:

导入java.util.Scanner;
类名
{
静态字符串;
静态字符串st1[]={”、“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”};
静态字符串st2[]={“百”、“千”、“千”、“千”、“千”、“千”};
静态字符串st3[]={“十”、“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”、“十九”};
静态字符串st4[]={“二十”、“三十”、“四十”、“五十”、“六”
   //     values entered        |     result
   //     2                     |     You have entered:Two
   //     10                    |     You have entered: Ten
   //     23                    |     You have entered: 23 which is not in range.

int i = Integer.parseInt(txtInput.getText());
    String[] namesOfNums = {"One", "Two", "Three","Four", "Five", "Six", "Seven","Eight", "Nine","Ten" };
    String output = "";
    if (i > 0 && i <=10) 
    {

        output = namesOfNums[i - 1];
        lblDisplay.setText("You have entered: "+ output);

    }
    else
    {
      lblDisplay.setText("You have entered: "+ i +" which is not range.");
    }
public class NumberToWord  

{
    private static final String[] specialNames = {
        "",
        " thousand",
        " million",
        " billion",
        " trillion",
        " quadrillion",
        " quintillion"
    };

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
    };

    private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
    };

    private String convertLessThanOneThousand(int number) {
        String current;

        if (number % 100 < 20){
            current = numNames[number % 100];
            number /= 100;
        }
        else {
            current = numNames[number % 10];
            number /= 10;

            current = tensNames[number % 10] + current;
            number /= 10;
        }
        if (number == 0) return current;
        return numNames[number] + " hundred" + current;
    }

    public String convert(int number) {

        if (number == 0) { return "zero"; }

        String prefix = "";

        if (number < 0) {
            number = -number;
            prefix = "negative";
        }

        String current = "";
        int place = 0;

        do {
            int n = number % 1000;
            if (n != 0){
                String s = convertLessThanOneThousand(n);
                current = s + specialNames[place] + current;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + current).trim();
    }

    public static void main(String[] args) {
        NumberToWord obj = new NumberToWord();
        System.out.println("*** " + obj.convert(123456789));
        System.out.println("*** " + obj.convert(-55));
    }
}
import java.util.Scanner;
class Name
{
    static String string;
    static String st1[]={"","one","two","three","four","five","six","seven","eight","nine"};
    static String st2[]={"hundred","thousand","lakh","crore"};
    static String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    static String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER");
        System.out.println(mac(in.nextInt())+" only");
    }
    public static String mac(int n)
    {
        int word;
        int nu=1;
        string="";
        while(n!=0)
        {
            switch(nu)
            {
            case 1:
                word=n%100;
                pass(word);
                if(n>100&&n%100!=0)
                {
                    show("and ");
                }
                n=n/100;
                break;
            case 2:
                word=n%10;
                if(word!=0)
                {
                    show(" ");
                    show(st2[0]);
                    show(" ");
                    pass(word);
                }
                n=n/10;
                break;
            case 3:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[1]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            case 4:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[2]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            case 5:
                word=n%100;
                if(word!=0)
                {
                    show(" ");
                    show(st2[3]);
                    show(" ");
                    pass(word);
                }
                n=n/100;
                break;
            }
            nu++;
        }
        return string;
    }
    public static void pass(int n)
    {
        int word,q;
        if(n<10)
        {
            show(st1[n]);
        }
        if(n>9&&n<20)
        {
            show(st3[n-10]);
        }
        if(n>19)
        {
            word=n%10;
            if(word==0)
            {
                q=n/10;
                show(st4[q-2]);
            }
            else
            {
                q=n/10;
                show(st1[word]);
                show(" ");
                show(st4[q-2]);
            }
        }
    }
    public static void show(String s)
    {
        String st=string;
        string=s+st;
    }
}
import java.util.Scanner;

public class number to words  {

  static String string;
  static String st1[]={"","one","two","three","four","five","six","seven","eight","nine"};
  static String st2[]={"hundred","thousand","lakh","crore"};
  static String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
  static String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

  public static void main(String args[])
  {
    Scanner in=new Scanner(System.in);
    System.out.println("ENTER THE NUMBER");
    System.out.println(mac(in.nextInt())+" only");
  }
  public static String mac(int n)
  {
    int word;
    int nu=1;
    string="";
    while(n!=0)
    {
        switch(nu)
        {
        case 1:
            word=n%100;
            pass(word);
            if(n>100&&n%100!=0)
            {
                show("and ");
            }
            n=n/100;
            break;
        case 2:
            word=n%10;
            if(word!=0)
            {
                show(" ");
                show(st2[0]);
                show(" ");
                pass(word);
            }
            n=n/10;
            break;
        case 3:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[1]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        case 4:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[2]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        case 5:
            word=n%100;
            if(word!=0)
            {
                show(" ");
                show(st2[3]);
                show(" ");
                pass(word);
            }
            n=n/100;
            break;
        }
        nu++;
    }
    return string;
  }
  public static void pass(int n)
  {
    int word,q;
    if(n<10)
    {
        show(st1[n]);
    }
    if(n>9&&n<20)
    {
        show(st3[n-10]);
    }
    if(n>19)
    {
        word=n%10;
        if(word==0)
        {
            q=n/10;
            show(st4[q-2]);
        }
        else
        {
            q=n/10;
            show(st1[word]);
            show(" ");
            show(st4[q-2]);
        }
    }
  }
  public static void show(String s)
  {
    String st=string;
    string=s+st;
  }
}
public class WordToNumber {

static String string;
static String st1[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
static int st2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

public static int mac(String n) {
    int digit = 0;
    try {
        if(Arrays.asList(st1).contains(n.toLowerCase())){
            int a = Arrays.asList(st1).indexOf(n.toLowerCase());
            digit = st2[a];
        }
        else{
            return -1;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
    return digit;
}
static Map<Long, String> map1 = new HashMap<Long, String>();

public static void main(String[] args) {
    Long num = new Long(300001);
    print(num, num.toString().length());
}

private static void print(Long num, int length) {
    if (length == 0) {
        System.out.println("Enter correct number");
    } else if (length == 1 || num <= 9) {
        printOneDigit(num);
    } else if (length == 2 || num <= 99) {
        printTwoDigit(num);
    } else if (length == 3 || num <= 999) {
        printThreeDigit(num);
    } else if (length == 4 || num <= 9999) {
        printFourDigit(num);
    } else if (length == 5 && num <= 99999) {
        printFiveDigit(num);
    } else if (length == 6 || num <= 999999) {
        printSixDigit(num);
    } else if (length == 7 || num <= 9999999) {
        printSevenDigit(num);
    } else if (length == 7 || num <= 99999999) {
        printEightDigit(num);
    } else
        System.out.println("Number is not valid....!!!!!");
}

private static void printOneDigit(Long num) {
    System.out.print(map1.get(num) != null ? map1.get(num) : "" + " ");
}

private static void printTwoDigit(Long num) {
    if (num % 10 == 0)
        System.out.print(map1.get(num) + "");
    else if (num < 20) {
        System.out.print(map1.get(num) + " ");
    } else {
        System.out.print(map1.get(num - num % 10) != null ? map1.get(num - num % 10) : "");
        System.out.print(" ");
        printOneDigit(num % 10);
    }
}

private static void printThreeDigit(Long num) {
    if (num % 10 == 0 && num % 100 == 0)
        System.out.print(map1.get(num / 100) + " HUNDRED");
    else {
        System.out.print(map1.get(num / 100) + " HUNDRED ");
        print(num % 100, 2);
    }
}

private static void printFourDigit(Long num) {
    if (num % 10 == 0 && num % 100 == 0 && num % 1000 == 0)
        System.out.print(map1.get(num / 1000) + " THOUSAND ");
    else {
        System.out.print(map1.get(num / 1000) + " THOUSAND ");
        print(num % 1000, 3);
    }
}

private static void printFiveDigit(Long num) {
    if (num / 1000 > 0) {
        printTwoDigit(num / 1000);
        System.out.print(" THOUSAND ");
    }
    if ((num % 1000) > 99) {
        printThreeDigit(num % 1000);
    } else if ((num % 1000) > 9) {
        printTwoDigit(num % 100);
    } else if (num % 10 > 0) {
        printOneDigit(num % 10);
    }
}

private static void printSixDigit(Long num) {
    if (num / 100000 > 0) {
        printOneDigit(num / 100000);
        System.out.print(" LACK ");
    }
    if (num > 0)
        printFiveDigit(num % 100000);
}

private static void printSevenDigit(Long num) {
    if (num / 100000 > 0) {
        printTwoDigit(num / 100000);
        System.out.print(" LACK ");
    }
    if (num % 100000 > 9999) {
        printFiveDigit(num % 100000);
    } else if ((num % 10000) > 999) {
        printFourDigit(num % 10000);
    } else if ((num % 1000) > 99) {
        printThreeDigit(num % 1000);
    } else if ((num % 1000) > 9) {
        printTwoDigit(num % 100);
    } else if (num % 10 > 0) {
        printOneDigit(num % 10);
    }
}

private static void printEightDigit(Long num) {
    printOneDigit(num / 10000000);
    System.out.print(" CRORE ");
    if (num > 0)
        printSevenDigit(num % 10000000);
}

static {
    map1.put(0L, "");
    map1.put(1L, "ONE");
    map1.put(2L, "TWO");
    map1.put(3L, "THREE");
    map1.put(4L, "FOUR");
    map1.put(5L, "FIVE");
    map1.put(6L, "SIX");
    map1.put(7L, "SEVEN");
    map1.put(8L, "EIGHT");
    map1.put(9L, "NINE");
    map1.put(10L, "TEN");
    map1.put(11L, "ELEVEN");
    map1.put(12L, "TWELVE");
    map1.put(13L, "THIRTEEN");
    map1.put(14L, "FOURTEEN");
    map1.put(15L, "FIFTEEN");
    map1.put(16L, "SIXTEEN");
    map1.put(17L, "SEVENTEEN");
    map1.put(18L, "EIGHTEEN");
    map1.put(19L, "NINTEEN");
    map1.put(20L, "TWINITY");
    map1.put(30L, "THIRTY");
    map1.put(40L, "FOURTY");
    map1.put(50L, "FIFTY");
    map1.put(60L, "SIXTY");
    map1.put(70L, "SEVENTY");
    map1.put(80L, "EIGHTY");
    map1.put(90L, "NINTY");
}
package com.number_to_word;

import java.util.Scanner;

/**
 * This program is to get the word value of given number. 
 * <p>Number can be ranges from 0 to 66th power of 10 (66 digits).</p>
 * Number can be passed through Scanner and it will print the word value of that number.
 * 
 * @author DHARAMRAJSINGH
 * 
 */
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter number: ");
        String n = sc.nextLine();
        System.out.println(getWord(n));
        sc.close();
    }

    /**
     * This method is using array inside it and this array is 2D array contains word values.
     * <p>This array ranges from arr[0][0] to arr[9][9]. </p>
     * <p>The array is initialized in a way that it will return values like, for arr[0][0] returns zero 
     * </br> arr[0][1] returns one, arr[2][1] returns twenty one, arr[9][5] returns ninety five, and so on. </p>
     * The logic is the number <strong>n</strong> is passed into it and divided by 10, the quotient will be index0 and remainder will be index1,
     * and return arr[index0][index1].
     * </br>  
     * @param n will be in the range of 0 to 99
     * @return the word value ranges from 0 to 99
     */
    private static String getValue(int n) {
        int index0 = 0, index1 = 0;
        if(n < 0 || n > 99) {
            throw new IllegalArgumentException("number cannot be less than zero or greater than 99");
        }
        if(n < 10) {
            index1 = n;
        } else {
            index1 = n%10;
            index0 = n/10;
        }
        String arr [][] = {
                // for "zero", "one" .... "nine"
                {"शुन्य",   "एकः",  "द्वौ", "त्रयः",    "चत्वारः",  "पञ्च", "षट्",  "सप्त", "अष्ट", "नव"},
                // for "ten", "eleven" .... "nineteen"
                {"दश",  "एकादशम्",  "द्वादशम्", "त्रयोदशम्",    "चतुर्दशम्",    "पञ्चदशम्", "षोडशम्",   "सप्तदशम्", "अष्टादशम्", "नवदशम्"},
                // for "twenty", "twenty one" .... "twenty nine"
                {"विंशति",  "एकाविंशति",    "द्वाविंशति",   "त्रयोविंशति",  "चतुर्विंशति",  "पञ्चविंशति",   "षड्विंशति",    "सप्तविंशति",   "अष्टाविंशति", "नवविंशति"},
                // for "thirty", "twenty one" .... "thirty nine"
                {"त्रिंशत्",    "एकत्रिंशत्",   "द्वात्रिंशत्", "त्रयत्रिंशत्", "चतुस्त्रिंशत्",    "पञ्चत्रिंशत्", "षट्त्रिंशत्",  "सप्तत्रिंशत्", "अष्टात्रिंशत्", "एकोनचत्वारिंशत्"},
                {"चत्वारिंशत्", "एकचत्वारिंशत्",    "द्विचत्वारिंशत्",  "त्रिचत्वारिंशत्",  "चतुश्चत्वारिंशत्", "पञ्चचत्वारिंशत्",  "षट्चत्वारिंशत्", "सप्तचत्वारिंशत्",    "अष्टचत्वारिंशत्",  "एकोनपञ्चाशत्"},
                {"पञ्चाशत्",    "एकपञ्चाशत्",   "द्विपञ्चाशत्", "त्रिपञ्चाशत्", "चतुःपञ्चाशत्", "पञ्चपञ्चाशत्", "षट्पञ्चाशत्",  "सप्तपञ्चाशत्", "अष्टपञ्चाशत्","एकोनषष्ठिः"},
                {"षष्ठिः",      "एकषष्ठिः", "द्विषष्ठिः",   "त्रिषष्ठिः",   "चतुःषष्ठिः",   "पञ्चषष्ठिः",   "षट्षष्ठिः",    "सप्तषष्ठिः",   "अष्टषष्ठिः",   "एकोनसप्ततिः"},
                {"सप्ततिः", "एकसप्ततिः",    "द्विसप्ततिः",  "त्रिसप्ततिः",  "चतुःसप्ततिः",  "पञ्चसप्ततिः",  "षट्सप्ततिः",   "सप्तसप्ततिः","अष्टसप्ततिः","एकोनाशीतिः"},
                {"अशीतिः",  "एकाशीतिः", "द्वशीतिः", "त्र्यशीतिः",   "चतुरशीतिः",    "पञ्चाशीतिः",   "षडशीतिः",  "सप्ताशीतिः",   "अष्टाशीतिः","एकोननवतिः"},
                // for "ninety", "two" .... "ninety nine"
                {"नवतिः",   "एकनवतिः",  "द्विनवतिः",    "त्रिनवतिः",    "चतुर्नवतिः",   "पञ्चनवतिः",    "षण्णवतिः", "सप्तनवतिः",    "अष्टनवतिः","एकोनशतम्"}

        };
        return arr[index0][index1];
    }
    /**
     * This method returns the place value of the digit, like for index value 2 it will return hundred, 
     * for index value 3 and 4 it will thousand, for index value 5 and 6 it will lacs, and so on. 
     * <p>It return empty string for index 0 and 1 as it does not have specific name for that. 
     * @param n is index value of digit
     * @return place value of index (position like unit place, tens place etc.)
     */
    private static String getPlaceValue(int n) {
        String [] arr = {"", "", "शत", "सहस्त्र", "सहस्त्र", "लक्ष", "लक्ष", // this line is same as "", "", "hundred", "thousand", "thousand", "lac", "lac"
                "कोटि", "कोटि", "कोटि", "कोटि", "कोटि", // same as "crore" , "crore" , "crore" and so on
                "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", 
                "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु",
                "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", 
                "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद",
                "पद्म", "पद्म", "पद्म", "पद्म", "पद्म",
                "महापद्म", "महापद्म", "महापद्म", "महापद्म", "महापद्म", 
                "खर्व", "खर्व", "खर्व", "खर्व", "खर्व", 
                "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व",
                "समुद्र", "समुद्र", "समुद्र", "समुद्र", "समुद्र", 
                "ओघ", "ओघ", "ओघ", "ओघ", "ओघ", 
                "महोघ", "महोघ", "महोघ", "महोघ", "महोघ"
                };
        if(n >= arr.length) {
            throw new IllegalArgumentException("Not in range");
        } else {
            return arr[n];
        }
    }
    /**
     * This method takes index(position of digit) as the input and return the number of digits need to be taken in consideration of calculation.
     * <p> Like for index 0 only one digit will be passed to get it's word value, for index 1 two digits(00 to 99) need to be passed,
     * for index 2 inly one need to be passed and so on.
     * @param index position of digit
     * @return next index value
     */
    private static int numOfDigitsToInclude(int index) {
        int [] a = {1, 2, 1, 1, 2, 1, 2, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5, 
                1, 2, 3, 4, 5};
        return a[index];
    }
    /**
     * This method returns next index value for calculations need to be performed.
     * @param index
     * @return
     */
    private static int nextIndex ( int index) {
        int [] a = {-1, -1, 1, 2, 2, 4, 4, 6, 6, 6, 6, 6, 11, 11, 11, 11, 11,
                16, 16, 16, 16, 16,
                21, 21, 21, 21, 21, 
                26, 26, 26, 26, 26, 
                31, 31, 31, 31, 31, 
                36, 36, 36, 36, 36, 
                41, 41, 41, 41, 41, 
                46, 46, 46, 46, 46, 
                51, 51, 51, 51, 51, 
                56, 56, 56, 56, 56, 
                61, 61, 61, 61, 61
        };
        return a[index];
    }

    /**
     * This method takes number as input in String(as it exceeds the limit for long) form.
     * <p> Convert into array and loop over it to get word value for number.
     * @param num
     * @return
     */
    private static String getWord(String num) {
        if(num.length() == 0) {
            throw new IllegalArgumentException("Not valid");
        }
        if(num.equals("0")){
            return getValue(0);
        }
        String s = "" + num;
        int size = s.length();
        long [] a = new long [size];
        String n = num; int k=0;

        for(int j = num.length() -1 ; j >= 0; j--) {
            a[k++] = Integer.parseInt(""+ num.charAt(j) );
        }
        int i = a.length-1;
        int arg = 0;
        int include = 0;
        StringBuffer buffer = new StringBuffer();
        while(i >= 0) {
            arg = 0;
            include = numOfDigitsToInclude(i);
            if(include == 2) {
                arg = (int) ((int) 10*a[i] + a[i-1]);
            } else if(include == 1) {
                arg = (int) a[i];
            } else {
                for(int l = 1; l<=include; l++) {
                    arg =  (int) (10*arg + a[i+1-l]);
                }
                //recursion used for arg value more than 99
                buffer.append(getWord(""+ arg)).append(" ");
            }
            if(arg == 0) {
                i = nextIndex(i);
                if(i <= 0) {
                    continue;
                }
                continue;
            }
            if(include <= 2) {
                buffer.append(getValue(arg)).append( " ");
            }
            buffer.append(getPlaceValue(i)).append(" ");
            i = nextIndex(i);
        }
        return buffer.toString();
    }
}