1class NumberToWordExample1
2{
3//user-defined static method that converts a number into words
4static void numberToWords(char num[])
5{
6//determines the number of digits in the given number
7int len = num.length;
8//checks the given number has number or not
9if (len == 0)
10{
11//if the given number is empty prints the following statement
12System.out.println("The string is empty.");
13return;
14}
15//here, we have specified the length of the number to 4
16//it means that the number (that you want to convert) should be four or less than four digits
17if (len > 4)
18{
19//if the given number is more than four-digit number, it prints the following statement
20System.out.println("\n The given number has more than 4 digits.");
21return;
22}
23//string type array for one-digit numbers
24String[] onedigit = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
25//string type array for two digits numbers
26//the first index is empty because it makes indexing easy
27String[] twodigits = new String[] {"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
28//string type array of tens multiples
29//the first two indexes are empty because it makes indexing easy
30String[] multipleoftens = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
31//string type array of power of tens
32String[] poweroftens = new String[] {"Hundred", "Thousand"};
33//Used for debugging purpose only
34//the valueOf() method returns the string representation of the character array argument
35System.out.print(String.valueOf(num) + ": ");
36//checks whether the length of the given string is one or not
37if (len == 1)
38{
39//if the above condition returns true, it accesses the corresponding index and prints the value of that index
40//[num[0]-'0']: getting the number equal the decimal value of the character (assuming the char is the digit)
41System.out.println(onedigit[num[0]-'0']);
42return;
43}
44int x = 0;
45//executes until num does not become not '\0'
46while (x < num.length)
47{
48//executes if the length of the string is greater than equal to three
49if (len >= 3)
50{
51if (num[x] - '0' != 0)
52{
53System.out.print(onedigit[num[x] - '0'] + " ");
54//here length can be 3 or 4
55System.out.print(poweroftens[len - 3]+ " ");
56}
57//decrements the length of the string by 1
58--len;
59}
60//executes if the given number has two digits
61else
62{
63//the if-statement handles the numbers from 10 to 19 only
64if (num[x] - '0' == 1)
65{
66//adding the digits of the given number
67//the logic behind sum up the digits is that we will use the sum for accessing the index of the array
68//for example: 17, sum of digits = 8
69//we will access the 8th index in twodigits[] array i.e. Seventeen
70int sum = num[x] - '0' + num[x + 1] - '0';
71System.out.println(twodigits[sum]);
72return;
73}
74//the else-if statement handles the number 20 only
75//compares the tens and unit place with 2 and 0 respectively
76else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0)
77{
78//executes if the above else-if condition returns true
79System.out.println("Twenty");
80return;
81}
82//the else block handles the numbers from 21 to 100
83else
84{
85int i = (num[x] - '0');
86if (i > 0)
87//prints the ith index element of the array multipleoftens[]
88System.out.print(multipleoftens[i]+ " ");
89else
90//prints space
91System.out.print("");
92//increments the variable i by 1
93++x;
94//checks whether the number is not equal to zero, it means the number has only a digit
95if (num[x] - '0' != 0)
96//prints the ith index element of the array onedigit[]
97System.out.println(onedigit[num[x] - '0']);
98}
99}
100//increments the variable i by 1
101++x;
102}
103}
104//main() method
105public static void main(String args[])
106{
107//calling the user-defined method and that invokes another predefined method toCharArray()
108//the method toCharArray() converts the given number into character array
109numberToWords("1111".toCharArray());
110numberToWords("673".toCharArray());
111numberToWords("85".toCharArray());
112numberToWords("5".toCharArray());
113numberToWords("0".toCharArray());
114numberToWords("20".toCharArray());
115numberToWords("1000".toCharArray());
116numberToWords("12345".toCharArray());
117//passing empty string
118numberToWords("".toCharArray());
119}
120}