//international number system
static String point = "point";
static String h = "hundred";
static String th = "thousand";
static String m = "million";
static String b = "billion";
static String
_0 = "",
_1 = "one",
_2 = "two",
_3 = "three",
_4 = "four",""
_5 = "five",
_6 = "six",
_7 = "seven",
_8 = "eight",
_9 = "nine";
static String
_00 = "",
_10 = "ten",
_20 = "twenty",
_30 = "thirty",
_40 = "forty",
_50 = "fifty",
_60 = "sixty",
_70 = "seventy",
_80 = "eighty",
_90 = "ninety";
static String
_11 = "eleven",
_12 = "twelve",
_13 = "thirteen",
_14 = "forteen",
_15 = "fifteen",
_16 = "sixteen",
_17 = "seventeen",
_18 = "eighteen",
_19 = "nineteen";
static String
_000 = "",
_100 = _1 + " " + h,
_200 = _2 + " " + h,
_300 = _3 + " " + h,
_400 = _4 + " " + h,
_500 = _5 + " " + h,
_600 = _6 + " " + h,
_700 = _7 + " " + h,
_800 = _8 + " " + h,
_900 = _9 + " " + h;
static String[][][] numberNames = new String[][][]{
{
{_0, _1, _2, _3, _4, _5, _6, _7, _8, _9},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+th, _2+" "+th, _3+" "+th, _4+" "+th, _5+" "+th,
_6+" "+th, _7+" "+th, _8+" "+th, _9+" "+th},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+m, _2+" "+m, _3+" "+m, _4+" "+m, _5+" "+m,
_6+" "+m, _7+" "+m, _8+" "+m, _9+" "+m},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+b, _2+" "+b, _3+" "+b, _4+" "+b, _5+" "+b,
_6+" "+b, _7+" "+b, _8+" "+b, _9+" "+b},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
}
};
//indian number system
static String l = "lakh";
static String c = "crore";
static String a = "arab";
static String[][][] indianNumberNames = new String[][][]{
{
{_0, _1, _2, _3, _4, _5, _6, _7, _8, _9},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+th, _2+" "+th, _3+" "+th, _4+" "+th, _5+" "+th,
_6+" "+th, _7+" "+th, _8+" "+th, _9+" "+th},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
},
{
{_0, _1+" "+l, _2+" "+l, _3+" "+l, _4+" "+l, _5+" "+l,
_6+" "+l, _7+" "+l, _8+" "+l, _9+" "+l},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
},
{
{_0, _1+" "+c, _2+" "+c, _3+" "+c, _4+" "+c, _5+" "+c,
_6+" "+c, _7+" "+c, _8+" "+c, _9+" "+c},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
},
{
{_0, _1+" "+a, _2+" "+a, _3+" "+a, _4+" "+a, _5+" "+a,
_6+" "+a, _7+" "+a, _8+" "+a, _9+" "+a},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
}
};
//the grouping of numbers,
//such as 1 000 000 000 where each group contains 3 digits,
//or 1 00 00 00 000 for indian number system,
//where each group contains 2 digits except the first group which has 3.
//may differ from one number system to another.
static int place_difference = 3;
//current number system
static int curNumSys = INTERNATIONAL_NUMBER_SYSTEM;
public static String convertToNumberName(String number){
String[] split = number.split("\.");
String num1, num2;
num1 = split[0];
num2 = split.length > 1 ? split[1] : "";
int place_counter = 0;
int step = 0;
Stack<String> numberNameBuilder = new Stack<>();
StringBuilder builder = new StringBuilder();
builder.append(num1);
num1 = (builder.reverse()).toString();
builder.setLength(0);
int counter = -1;
for(int i = 0; i < num1.length(); i++){
int c = Integer.parseInt(num1.charAt(i) + "");
counter++;
place_counter = counter % place_difference;
if (place_counter == 0){
step++;
//specific number system component, this is for indian number system
//find a generic approach at a later time
// if (step == 2){
// counter = 0;
// place_difference = 2;
// }
}
String name = getDataSet()[step - 1][place_counter][c];
numberNameBuilder.push(name);
}
while(numberNameBuilder.hasNext()){
builder.append(numberNameBuilder.pop() + " ");
}
String str = builder.toString();
str = refine(str);
builder.setLength(0);
builder.append(num2);
num2 = builder.reverse().toString();
builder.setLength(0);
//decimal conversion here
for(int i = 0; i < num2.length(); i++){
int c = Integer.parseInt(num2.charAt(i) + "");
numberNameBuilder.push(getDataSet()[0][0][c]);
}
while(numberNameBuilder.hasNext()){
builder.append(numberNameBuilder.pop() + " ");
}
str += point + " ";
str += builder.toString();
return str;
}
private static String refine(String str){
str = str.replace(_10 + " " + _1, _11);
str = str.replace(_10 + " " + _2, _12);
str = str.replace(_10 + " " + _3, _13);
str = str.replace(_10 + " " + _4, _14);
str = str.replace(_10 + " " + _5, _15);
str = str.replace(_10 + " " + _6, _16);
str = str.replace(_10 + " " + _7, _17);
str = str.replace(_10 + " " + _8, _18);
str = str.replace(_10 + " " + _9, _19);
return str;
}
private static String[][][] getDataSet(){
switch(curNumSys){
case INDIAN_NUMBER_SYSTEM :
return indianNumberNames;
case INTERNATIONAL_NUMBER_SYSTEM :
return numberNames;
}
return numberNames;
}
Just simply call the convertToNumberName() function and give the numbers as string,to change from international to Indian, simply uncomment the commented lines in the function mentioned above, to add more number systems, more data sets and minor tweaking in the function will be required.
The stack mentioned here is just a simple LIFO structure and with ordinary stack functions, one could use their own custom implementation of the stack here or just import from Java.
The limit can be expanded further, for example, international number system only has capacity to convert up-to 999 billions, however, the data set can be expanded further to add trillions and quadrillions and more, it can go as far as the dataset that is the 3d array in code above allows it to.
In this post we’ll see a Java program to convert numbers to words. For example, in the Java program if number 5467 is passed
then the output in words should be- Five thousand four hundred sixty seven.
Number systems
In Indian system numbers are called differently and even placement is different than in the international system.
For example-
11,23,45,657- Eleven crore twenty three lakhs forty five thousand six hundred fifty seven
As you can see the numbers here are Crore, Lakhs also the digits are grouped in twos except the hundreds where three digits are grouped.
In international system same number will be written as-
112,345,657- One hundred twelve million three hundred forty five thousand six hundred fifty seven
Here you can notice that the digits are grouped in threes and the digit group separator (hundred, thousand, million…) is
placed after every three digits.
We’ll have Java program for converting numbers to words for both Indian and international system and how digits are grouped
forms the logic for the programs.
Converting number to words in Java – International System
As already stated above, in the program we’ll have to get the three digits of the number in each iteration and put the
correct place value after those three digits.
In the program we’ll also have different arrays for the numbers.
public class ConvertNumToWord { private static final String[] units = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine" }; private static final String[] twoDigits = { " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; private static final String[] tenMultiples = { "", "", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }; private static final String[] placeValues = { " ", " thousand", " million", " billion", " trillion" }; private static String convertNumber(long number) { String word = ""; int index = 0; do { // take 3 digits in each iteration int num = (int)(number % 1000); if (num != 0){ String str = ConversionForUptoThreeDigits(num); word = str + placeValues[index] + word; } index++; // next 3 digits number = number/1000; } while (number > 0); return word; } private static String ConversionForUptoThreeDigits(int number) { String word = ""; int num = number % 100; if(num < 10){ word = word + units[num]; } else if(num < 20){ word = word + twoDigits[num%10]; }else{ word = tenMultiples[num/10] + units[num%10]; } word = (number/100 > 0)? units[number/100] + " hundred" + word : word; return word; } public static void main(String[] args) { System.out.println("1234123456789- " + convertNumber(1234123456789L)); System.out.println("123456789- " + convertNumber(123456789)); System.out.println("37565820- " + convertNumber(37565820)); System.out.println("9341947- " + convertNumber(9341947)); System.out.println("37000- " + convertNumber(37000)); System.out.println("1387- " + convertNumber(1387)); System.out.println("10- " + convertNumber(10)); System.out.println("41- " + convertNumber(41)); } }
Output
1234123456789- one trillion two hundred thirty four billion one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine 123456789- one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine 37565820- thirty seven million five hundred sixty five thousand eight hundred twenty 9341947- nine million three hundred forty one thousand nine hundred forty seven 37000- thirty seven thousand 1387- one thousand three hundred eighty seven 10- ten 41- forty one
Converting number to words in Java – Indian System
As already stated above, for Indian system in the program we’ll have to get the three digits of the number in first
iteration and then two digits in each iteration and put the correct place value after each iteration.
The array for placeValues will change as per Indian system.
public class ConvertNumToWord { private static final String[] units = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine" }; private static final String[] twoDigits = { " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; private static final String[] tenMultiples = { "", "", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }; private static final String[] placeValues = { "", " thousand", " lakh", " crore", " arab", " kharab" }; private static String convertNumber(long number) { String word = ""; int index = 0; boolean firstIteration = true; int divisor; do { divisor = firstIteration ? 1000 : 100; // take 3 or 2 digits based on iteration int num = (int)(number % divisor); if (num != 0){ String str = ConversionForUptoThreeDigits(num); word = str + placeValues[index] + word; } index++; // next batch of digits number = number/divisor; firstIteration = false; } while (number > 0); return word; } private static String ConversionForUptoThreeDigits(int number) { String word = ""; int num = number % 100; if(num < 10){ word = word + units[num]; } else if(num < 20){ word = word + twoDigits[num%10]; }else{ word = tenMultiples[num/10] + units[num%10]; } word = (number/100 > 0)? units[number/100] + " hundred" + word : word; return word; } public static void main(String[] args) { System.out.println("1234123456789- " + convertNumber(1234123456789L)); System.out.println("326776756- " + convertNumber(326776756)); System.out.println("37565820- " + convertNumber(37565820)); System.out.println("9341947- " + convertNumber(9341947)); System.out.println("37000- " + convertNumber(37000)); System.out.println("1387- " + convertNumber(1387)); System.out.println("10- " + convertNumber(10)); System.out.println("5- " + convertNumber(5)); } }
Output
1234123456789- twelve kharab thirty four arab twelve crore thirty four lakh fifty six thousand seven hundred eighty nine 326776756- thirty two crore sixty seven lakh seventy six thousand seven hundred fifty six 37565820- three crore seventy five lakh sixty five thousand eight hundred twenty 9341947- ninety three lakh forty one thousand nine hundred forty seven 37000- thirty seven thousand 1387- one thousand three hundred eighty seven 10- ten 5- five
That’s all for this topic Convert Numbers to Words Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
- Convert double to int in Java
- Arrange Non-Negative Integers to Form Largest Number — Java Program
- Split a String Java Program
- Converting String to int in Java
- Converting String to Enum Type in Java
You may also like-
- Printing Numbers in Sequence Using Threads Java Program
- How to Create PDF in Java Using OpenPDF
- Difference Between Two Dates in Java
- Creating Tar File And GZipping Multiple Files in Java
- How to Pass Command Line Arguments in Eclipse
- static Import in Java With Examples
- Java String Interview Questions And Answers
- Spring MVC Pagination Example Using PagedListHolder
next →
← prev
In this section, we will create a Java program that converts the given number into words. For example, if the given number is 54,297 then the output should be Fifty-Four Thousand Two Hundred Ninety-Seven. Let’s create a Java program for the same.
NumberToWordExample1.java
Output:
The number is not limited to four digits. Then what if the user enters more than 5 five digits numbers. The above program does not work for large digit numbers.
Converting Large Numbers into Words
The logic for converting the large digit numbers is quite different. Let’s see the approach to convert large digits numbers.
NumberToWordExample2.java
Output:
Converting Very Long Numbers into Words
The logic for the conversion of very long numbers into words is different from the previous one. Before moving ahead, it is important to learn the name of large numbers. The following table describes the name of the larger numbers.
Name | Short Scale (US, English Canada, modern British, Australia, and Eastern Europe) |
Long Scale (French Canada, older British, Western & Central Europe) |
---|---|---|
Million | 106 | 106 |
Milliard | Not used | 109 |
Billion | 109 | 1012 |
Billiard | Not used | 1015 |
Trillion | 1012 | 1018 |
Quadrillion | 1015 | 1024 |
Quintillion | 1018 | 1030 |
Sextillion | 1021 | 1036 |
Septillion | 1024 | 1042 |
Octillion | 1027 | 1048 |
Nonillion | 1030 | 1054 |
Decillion | 1033 | 1060 |
Undecillion | 1036 | 1066 |
Duodecillion | 1039 | 1072 |
Tredecillion | 1042 | 1078 |
Quattuordecillion | 1045 | 1084 |
Quindecillion | 1048 | 1090 |
Sexdecillion | 1051 | 1096 |
Septendecillion | 1054 | 10102 |
Octodecillion | 1057 | 10108 |
Novemdecillion | 1060 | 10114 |
Vigintillion | 1063 | 10120 |
Centillion | 10303 | 10600 |
Let’s create a Java program that converts very long numbers into words.
The following Java program also converts the negative and decimal numbers into words.
NumberToWordExample3.java
Output:
0 = zero 4 = four 10 = ten 12 = twelve 100 = one hundred 108 = one hundred eight 299 = two hundred ninety-nine 1000 = one thousand 1003 = one thousand three 2040 = two thousand forty 45213 = forty-five thousand two hundred thirteen 100000 = one hundred thousand 100005 = one hundred thousand five 100010 = one hundred thousand ten 202020 = two hundred two thousand twenty 202022 = two hundred two thousand twenty-two 999999 = nine hundred ninety-nine thousand nine hundred ninety-nine 1000000 = one million 1000001 = one million one 10000000 = ten million 10000007 = ten million seven 99999999 = ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine 9223372036854775807 = nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven -9223372036854775808 = minus nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred eight 0001.2 = one and two tenth 3.141592 = three and one hundred forty-one thousand five hundred ninety-two millionth
Even, we can convert 100 digits long number with the help of the above program.
Next TopicTypes of Garbage Collector in Java
← prev
next →
Java program to convert numbers to words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.NumberFormat; | |
public class NumberToWordsConverter { | |
public static final String[] units = { «», «One», «Two», «Three», «Four», | |
«Five», «Six», «Seven», «Eight», «Nine», «Ten», «Eleven», «Twelve», | |
«Thirteen», «Fourteen», «Fifteen», «Sixteen», «Seventeen», | |
«Eighteen», «Nineteen» }; | |
public static final String[] tens = { | |
«», // 0 | |
«», // 1 | |
«Twenty», // 2 | |
«Thirty», // 3 | |
«Forty», // 4 | |
«Fifty», // 5 | |
«Sixty», // 6 | |
«Seventy», // 7 | |
«Eighty», // 8 | |
«Ninety» // 9 | |
}; | |
public static String convert(final int n) { | |
if (n < 0) { | |
return «Minus « + convert(-n); | |
} | |
if (n < 20) { | |
return units[n]; | |
} | |
if (n < 100) { | |
return tens[n / 10] + ((n % 10 != 0) ? » « : «») + units[n % 10]; | |
} | |
if (n < 1000) { | |
return units[n / 100] + » Hundred» + ((n % 100 != 0) ? » « : «») + convert(n % 100); | |
} | |
if (n < 100000) { | |
return convert(n / 1000) + » Thousand» + ((n % 10000 != 0) ? » « : «») + convert(n % 1000); | |
} | |
if (n < 10000000) { | |
return convert(n / 100000) + » Lakh» + ((n % 100000 != 0) ? » « : «») + convert(n % 100000); | |
} | |
return convert(n / 10000000) + » Crore» + ((n % 10000000 != 0) ? » « : «») + convert(n % 10000000); | |
} | |
public static void main(final String[] args) { | |
int n; | |
n = 5; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 16; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 50; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 78; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 456; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 1000; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 99999; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 199099; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
n = 10005000; | |
System.out.println(NumberFormat.getInstance().format(n) + «='» + convert(n) + «‘»); | |
} | |
} |
Write code to convert a given number into words. For example, if “1234” is given as input, the output should be “one thousand two hundred thirty-four”.
Following is the implementation for the same. The code supports numbers up to 4 digits, i.e., numbers from 0 to 9999. Idea is to create arrays that store individual parts of output strings. One array is used for single digits, one for numbers from 10 to 19, one for 20, 30, 40, 50, .. etc, and one for powers of 10.
The given number is divided into two parts: the first two digits and the last two digits, and the two parts are printed separately.
C++
#include <bits/stdc++.h>
using
namespace
std;
void
convert_to_words(
char
* num)
{
int
len =
strlen
(
num);
if
(len == 0) {
fprintf
(stderr,
"empty stringn"
);
return
;
}
if
(len > 4) {
fprintf
(stderr,
"Length more than 4 is not supportedn"
);
return
;
}
char
* single_digits[]
= {
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
};
char
* two_digits[]
= {
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
};
char
* tens_multiple[] = {
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
};
char
* tens_power[] = {
"hundred"
,
"thousand"
};
cout <<
"n"
<< num <<
": "
;
if
(len == 1) {
cout << single_digits[*num -
'0'
] <<
"n"
;
return
;
}
while
(*num !=
''
) {
if
(len >= 3) {
if
(*num -
'0'
!= 0) {
cout << single_digits[*num -
'0'
] <<
" "
;
cout << tens_power[len - 3]
<<
" "
;
}
--len;
}
else
{
if
(*num ==
'1'
) {
int
sum = *num -
'0'
+ *(num + 1) -
'0'
;
cout << two_digits[sum] <<
"n"
;
return
;
}
else
if
(*num ==
'2'
&& *(num + 1) ==
'0'
) {
cout <<
"twentyn"
;
return
;
}
else
{
int
i = *num -
'0'
;
string
tm
= i ? tens_multiple[i] :
""
;
cout <<
tm
<<
" "
;
++num;
if
(*num !=
'0'
)
cout << single_digits[*num -
'0'
]
<<
" "
;
}
}
++num;
}
}
int
main()
{
convert_to_words(
"9923"
);
convert_to_words(
"523"
);
convert_to_words(
"89"
);
convert_to_words(
"8"
);
return
0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
convert_to_words(
char
* num)
{
int
len =
strlen
(
num);
if
(len == 0) {
fprintf
(stderr,
"empty stringn"
);
return
;
}
if
(len > 4) {
fprintf
(stderr,
"Length more than 4 is not supportedn"
);
return
;
}
char
* single_digits[]
= {
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
};
char
* two_digits[]
= {
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
};
char
* tens_multiple[] = {
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
};
char
* tens_power[] = {
"hundred"
,
"thousand"
};
printf
(
"n%s: "
, num);
if
(len == 1) {
printf
(
"%sn"
, single_digits[*num -
'0'
]);
return
;
}
while
(*num !=
''
) {
if
(len >= 3) {
if
(*num -
'0'
!= 0) {
printf
(
"%s "
, single_digits[*num -
'0'
]);
printf
(
"%s "
,
tens_power[len - 3]);
}
--len;
}
else
{
if
(*num ==
'1'
) {
int
sum = *num -
'0'
+ *(num + 1) -
'0'
;
printf
(
"%sn"
, two_digits[sum]);
return
;
}
else
if
(*num ==
'2'
&& *(num + 1) ==
'0'
) {
printf
(
"twentyn"
);
return
;
}
else
{
int
i = *num -
'0'
;
printf
(
"%s "
, i ? tens_multiple[i] :
""
);
++num;
if
(*num !=
'0'
)
printf
(
"%s "
,
single_digits[*num -
'0'
]);
}
}
++num;
}
}
int
main(
void
)
{
convert_to_words(
"9923"
);
convert_to_words(
"523"
);
convert_to_words(
"89"
);
convert_to_words(
"8"
);
return
0;
}
Java
class
GFG {
static
void
convert_to_words(
char
[] num)
{
int
len = num.length;
if
(len ==
0
) {
System.out.println(
"empty string"
);
return
;
}
if
(len >
4
) {
System.out.println(
"Length more than 4 is not supported"
);
return
;
}
String[] single_digits =
new
String[] {
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
};
String[] two_digits =
new
String[] {
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
};
String[] tens_multiple =
new
String[] {
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
};
String[] tens_power
=
new
String[] {
"hundred"
,
"thousand"
};
System.out.print(String.valueOf(num) +
": "
);
if
(len ==
1
) {
System.out.println(single_digits[num[
0
] -
'0'
]);
return
;
}
int
x =
0
;
while
(x < num.length) {
if
(len >=
3
) {
if
(num[x] -
'0'
!=
0
) {
System.out.print(
single_digits[num[x] -
'0'
] +
" "
);
System.out.print(tens_power[len -
3
]
+
" "
);
}
--len;
}
else
{
if
(num[x] -
'0'
==
1
) {
int
sum
= num[x] -
'0'
+ num[x +
1
] -
'0'
;
System.out.println(two_digits[sum]);
return
;
}
else
if
(num[x] -
'0'
==
2
&& num[x +
1
] -
'0'
==
0
) {
System.out.println(
"twenty"
);
return
;
}
else
{
int
i = (num[x] -
'0'
);
if
(i >
0
)
System.out.print(tens_multiple[i]
+
" "
);
else
System.out.print(
""
);
++x;
if
(num[x] -
'0'
!=
0
)
System.out.println(
single_digits[num[x] -
'0'
]);
}
}
++x;
}
}
public
static
void
main(String[] args)
{
convert_to_words(
"9923"
.toCharArray());
convert_to_words(
"523"
.toCharArray());
convert_to_words(
"89"
.toCharArray());
convert_to_words(
"8"
.toCharArray());
}
}
Python3
def
convert_to_words(num):
l
=
len
(num)
if
(l
=
=
0
):
print
(
"empty string"
)
return
if
(l >
4
):
print
(
"Length more than 4 is not supported"
)
return
single_digits
=
[
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
]
two_digits
=
["
", "
ten
", "
eleven
", "
twelve",
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
]
tens_multiple
=
["
", "
", "
twenty
", "
thirty
", "
forty",
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
]
tens_power
=
[
"hundred"
,
"thousand"
]
print
(num,
":"
, end
=
" "
)
if
(l
=
=
1
):
print
(single_digits[
ord
(num[
0
])
-
48
])
return
x
=
0
while
(x <
len
(num)):
if
(l >
=
3
):
if
(
ord
(num[x])
-
48
!
=
0
):
print
(single_digits[
ord
(num[x])
-
48
],
end
=
" "
)
print
(tens_power[l
-
3
], end
=
" "
)
l
-
=
1
else
:
if
(
ord
(num[x])
-
48
=
=
1
):
sum
=
(
ord
(num[x])
-
48
+
ord
(num[x
+
1
])
-
48
)
print
(two_digits[
sum
])
return
elif
(
ord
(num[x])
-
48
=
=
2
and
ord
(num[x
+
1
])
-
48
=
=
0
):
print
(
"twenty"
)
return
else
:
i
=
ord
(num[x])
-
48
if
(i >
0
):
print
(tens_multiple[i], end
=
" "
)
else
:
print
("
", end="
")
x
+
=
1
if
(
ord
(num[x])
-
48
!
=
0
):
print
(single_digits[
ord
(num[x])
-
48
])
x
+
=
1
convert_to_words(
"9923"
)
convert_to_words(
"523"
)
convert_to_words(
"89"
)
convert_to_words(
"8"
)
C#
using
System;
class
GFG {
static
void
convert_to_words(
char
[] num)
{
int
len = num.Length;
if
(len == 0) {
Console.WriteLine(
"empty string"
);
return
;
}
if
(len > 4) {
Console.WriteLine(
"Length more than "
+
"4 is not supported"
);
return
;
}
string
[] single_digits =
new
string
[] {
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
};
string
[] two_digits =
new
string
[] {
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
};
string
[] tens_multiple =
new
string
[] {
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
};
string
[] tens_power
=
new
string
[] {
"hundred"
,
"thousand"
};
Console.Write((
new
string
(num)) +
": "
);
if
(len == 1) {
Console.WriteLine(single_digits[num[0] -
'0'
]);
return
;
}
int
x = 0;
while
(x < num.Length) {
if
(len >= 3) {
if
(num[x] -
'0'
!= 0) {
Console.Write(
single_digits[num[x] -
'0'
] +
" "
);
Console.Write(tens_power[len - 3]
+
" "
);
}
--len;
}
else
{
if
(num[x] -
'0'
== 1) {
int
sum = num[x] -
'0'
+ num[x + 1] -
'0'
;
Console.WriteLine(two_digits[sum]);
return
;
}
else
if
(num[x] -
'0'
== 2
&& num[x + 1] -
'0'
== 0) {
Console.WriteLine(
"twenty"
);
return
;
}
else
{
int
i = (num[x] -
'0'
);
if
(i > 0)
Console.Write(tens_multiple[i]
+
" "
);
else
Console.Write(
""
);
++x;
if
(num[x] -
'0'
!= 0)
Console.WriteLine(
single_digits[num[x] -
'0'
]);
}
}
++x;
}
}
public
static
void
Main()
{
convert_to_words(
"9923"
.ToCharArray());
convert_to_words(
"523"
.ToCharArray());
convert_to_words(
"89"
.ToCharArray());
convert_to_words(
"8"
.ToCharArray());
}
}
PHP
<?php
function
convert_to_words(
$num
)
{
$len
=
strlen
(
$num
);
if
(
$len
== 0)
{
echo
"empty stringn"
;
return
;
}
if
(
$len
> 4)
{
echo
"Length more than 4 "
.
"is not supportedn"
;
return
;
}
$single_digits
=
array
(
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
);
$two_digits
=
array
(
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
);
$tens_multiple
=
array
(
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
);
$tens_power
=
array
(
"hundred"
,
"thousand"
);
echo
$num
.
": "
;
if
(
$len
== 1)
{
echo
$single_digits
[
$num
[0] -
'0'
] .
" n"
;
return
;
}
$x
= 0;
while
(
$x
<
strlen
(
$num
))
{
if
(
$len
>= 3)
{
if
(
$num
[
$x
]-
'0'
!= 0)
{
echo
$single_digits
[
$num
[
$x
] -
'0'
] .
" "
;
echo
$tens_power
[
$len
- 3] .
" "
;
}
--
$len
;
}
else
{
if
(
$num
[
$x
] -
'0'
== 1)
{
$sum
=
$num
[
$x
] -
'0'
+
$num
[
$x
] -
'0'
;
echo
$two_digits
[
$sum
] .
" n"
;
return
;
}
else
if
(
$num
[
$x
] -
'0'
== 2 &&
$num
[
$x
+ 1] -
'0'
== 0)
{
echo
"twentyn"
;
return
;
}
else
{
$i
=
$num
[
$x
] -
'0'
;
if
(
$i
> 0)
echo
$tens_multiple
[
$i
] .
" "
;
else
echo
""
;
++
$x
;
if
(
$num
[
$x
] -
'0'
!= 0)
echo
$single_digits
[
$num
[
$x
] -
'0'
] .
" n"
;
}
}
++
$x
;
}
}
convert_to_words(
"9923"
);
convert_to_words(
"523"
);
convert_to_words(
"89"
);
convert_to_words(
"8"
);
?>
Javascript
<script>
function
convert_to_words(num){
let l = num.length
if
(l == 0){
document.write(
"empty string"
,
"</br>"
)
return
}
if
(l > 4){
document.write(
"Length more than 4 is not supported"
,
"</br>"
)
return
}
let single_digits = [
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
]
let two_digits = [
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
]
let tens_multiple = [
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
]
let tens_power = [
"hundred"
,
"thousand"
]
document.write(num,
":"
,
" "
)
if
(l == 1){
document.write(single_digits[num.charCodeAt(0) - 48],
"</br>"
)
return
}
let x = 0
while
(x < num.length){
if
(l >= 3){
if
(num.charCodeAt(x) - 48 != 0){
document.write(single_digits[num.charCodeAt(x) - 48],
" "
)
document.write(tens_power[l - 3],
" "
)
}
l -= 1
}
else
{
if
(num.charCodeAt(x) - 48 == 1){
sum = (num.charCodeAt(x) - 48 + num.charCodeAt(x+1) - 48)
document.write(two_digits[sum],
"</br>"
)
return
}
else
if
(num.charCodeAt(x) - 48 == 2 &&
num.charCodeAt(x + 1) - 48 == 0){
document.write(
"twenty"
,
"</br>"
)
return
}
else
{
i = num.charCodeAt(x) - 48
if
(i > 0)
document.write(tens_multiple[i], end=
" "
)
else
document.write(
""
, end=
""
)
x += 1
if
(num.charCodeAt(x) - 48 != 0)
document.write(single_digits[num.charCodeAt(x) - 48],
"</br>"
)
}
}
x += 1
}
}
convert_to_words(
"9923"
)
convert_to_words(
"523"
)
convert_to_words(
"89"
)
convert_to_words(
"8"
)
</script>
Output
9923: nine thousand nine hundred twenty three 523: five hundred twenty three 89: eighty nine 8: eight
Time Complexity: O(N) [N is the length of the string]
Auxiliary Space: O(1)
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Approach 2: The following code supports numbers up to 15 digits, i.e., numbers from 0 to trillions. The code prints according to the western number system.
Below is the implementation:
C++
#include <bits/stdc++.h>
using
namespace
std;
string numberToWords(
long
long
int
n)
{
long
long
int
limit = 1000000000000, curr_hun, t = 0;
if
(n == 0)
return
(
"Zero"
);
string multiplier[] = {
""
,
"Trillion"
,
"Billion"
,
"Million"
,
"Thousand"
};
string first_twenty[] = {
""
,
"One"
,
"Two"
,
"Three"
,
"Four"
,
"Five"
,
"Six"
,
"Seven"
,
"Eight"
,
"Nine"
,
"Ten"
,
"Eleven"
,
"Twelve"
,
"Thirteen"
,
"Fourteen"
,
"Fifteen"
,
"Sixteen"
,
"Seventeen"
,
"Eighteen"
,
"Nineteen"
};
string tens[]
= {
""
,
"Twenty"
,
"Thirty"
,
"Forty"
,
"Fifty"
,
"Sixty"
,
"Seventy"
,
"Eighty"
,
"Ninety"
};
if
(n < 20)
return
(first_twenty[n]);
string answer =
""
;
for
(
long
long
int
i = n; i > 0;
i %= limit, limit /= 1000) {
curr_hun = i / limit;
while
(curr_hun == 0) {
i %= limit;
limit /= 1000;
curr_hun = i / limit;
++t;
}
if
(curr_hun > 99)
answer += (first_twenty[curr_hun / 100]
+
" Hundred "
);
curr_hun = curr_hun % 100;
if
(curr_hun > 0 && curr_hun < 20)
answer += (first_twenty[curr_hun] +
" "
);
else
if
(curr_hun % 10 == 0 && curr_hun != 0)
answer += (tens[curr_hun / 10 - 1] +
" "
);
else
if
(curr_hun > 20 && curr_hun < 100)
answer += (tens[curr_hun / 10 - 1] +
" "
+ first_twenty[curr_hun % 10] +
" "
);
if
(t < 4)
answer += (multiplier[++t] +
" "
);
}
return
(answer);
}
int
main()
{
long
long
int
n = 36;
cout << numberToWords(n) <<
'n'
;
n = 123456789;
cout << numberToWords(n) <<
'n'
;
n = 10101010110001;
cout << numberToWords(n) <<
'n'
;
n = 999999999;
cout << numberToWords(n) <<
'n'
;
return
0;
}
Java
public
class
GFG {
static
String numberToWords(
long
n)
{
long
limit = 1000000000000L, curr_hun, t =
0
;
if
(n ==
0
)
return
(
"Zero"
);
String multiplier[] = {
""
,
"Trillion"
,
"Billion"
,
"Million"
,
"Thousand"
};
String first_twenty[] = {
""
,
"One"
,
"Two"
,
"Three"
,
"Four"
,
"Five"
,
"Six"
,
"Seven"
,
"Eight"
,
"Nine"
,
"Ten"
,
"Eleven"
,
"Twelve"
,
"Thirteen"
,
"Fourteen"
,
"Fifteen"
,
"Sixteen"
,
"Seventeen"
,
"Eighteen"
,
"Nineteen"
};
String tens[] = {
""
,
"Twenty"
,
"Thirty"
,
"Forty"
,
"Fifty"
,
"Sixty"
,
"Seventy"
,
"Eighty"
,
"Ninety"
};
if
(n < 20L)
return
(first_twenty[(
int
)n]);
String answer =
""
;
for
(
long
i = n; i >
0
; i %= limit, limit /=
1000
) {
curr_hun = i / limit;
while
(curr_hun ==
0
) {
i %= limit;
limit /=
1000
;
curr_hun = i / limit;
++t;
}
if
(curr_hun >
99
)
answer += (first_twenty[(
int
)curr_hun /
100
]
+
" Hundred "
);
curr_hun = curr_hun %
100
;
if
(curr_hun >
0
&& curr_hun <
20
)
answer
+= (first_twenty[(
int
)curr_hun] +
" "
);
else
if
(curr_hun %
10
==
0
&& curr_hun !=
0
)
answer
+= (tens[(
int
)curr_hun /
10
-
1
] +
" "
);
else
if
(curr_hun >
20
&& curr_hun <
100
)
answer
+= (tens[(
int
)curr_hun /
10
-
1
] +
" "
+ first_twenty[(
int
)curr_hun %
10
]
+
" "
);
if
(t <
4
)
answer += (multiplier[(
int
)++t] +
" "
);
}
return
(answer);
}
public
static
void
main(String args[])
{
long
n = 36L;
System.out.println(numberToWords(n));
n =
123456789
;
System.out.println(numberToWords(n));
n = 10101010110001L;
System.out.println(numberToWords(n));
n =
999999999
;
System.out.println(numberToWords(n));
}
}
Python
def
numberToWords(n):
limit, t
=
1000000000000
,
0
if
(n
=
=
0
):
print
(
"zero"
)
return
multiplier
=
["
", "
Trillion
", "
Billion
", "
Million
", "
Thousand"]
first_twenty
=
["
", "
One
", "
Two",
"Three"
,
"Four"
,
"Five"
,
"Six"
,
"Seven"
,
"Eight"
,
"Nine"
,
"Ten"
,
"Eleven"
,
"Twelve"
,
"Thirteen"
,
"Fourteen"
,
"Fifteen"
,
"Sixteen"
,
"Seventeen"
,
"Eighteen"
,
"Nineteen"
]
tens
=
["
", "
Twenty
", "
Thirty
", "
Forty
", "
Fifty",
"Sixty"
,
"Seventy"
,
"Eighty"
,
"Ninety"
]
if
(n <
20
):
print
(first_twenty[n])
return
answer
=
""
i
=
n
while
(i >
0
):
curr_hun
=
i
/
/
limit
while
(curr_hun
=
=
0
):
i
%
=
limit
limit
/
=
1000
curr_hun
=
i
/
/
limit
t
+
=
1
if
(curr_hun >
99
):
answer
+
=
(first_twenty[curr_hun
/
/
100
]
+
" tensundred "
)
curr_hun
=
curr_hun
%
100
if
(curr_hun >
0
and
curr_hun <
20
):
answer
+
=
(first_twenty[curr_hun]
+
" "
)
elif
(curr_hun
%
10
=
=
0
and
curr_hun !
=
0
):
answer
+
=
(tens[(curr_hun
/
/
10
)
-
1
]
+
" "
)
elif
(curr_hun >
19
and
curr_hun <
100
):
answer
+
=
(tens[(curr_hun
/
/
10
)
-
1
]
+
" "
+
first_twenty[curr_hun
%
10
]
+
" "
)
if
(t <
4
):
answer
+
=
(multiplier[t]
+
" "
)
i
=
i
%
limit
limit
=
limit
/
/
1000
print
(answer)
n
=
36
numberToWords(n)
n
=
123456789
numberToWords(n)
n
=
10101010110001
numberToWords(n)
n
=
999999999
numberToWords(n)
C#
using
System;
public
class
numtowords
{
public
static
String numberToWords(
long
n)
{
var
limit = 1000000000000L;
long
curr_hun;
var
t = 0;
if
(n == 0)
{
return
(
"Zero"
);
}
String[] multiplier = {
""
,
"Trillion"
,
"Billion"
,
"Million"
,
"Thousand"
};
String[] first_twenty = {
""
,
"One"
,
"Two"
,
"Three"
,
"Four"
,
"Five"
,
"Six"
,
"Seven"
,
"Eight"
,
"Nine"
,
"Ten"
,
"Eleven"
,
"Twelve"
,
"Thirteen"
,
"Fourteen"
,
"Fifteen"
,
"Sixteen"
,
"Seventeen"
,
"Eighteen"
,
"Nineteen"
};
String[] tens = {
""
,
"Twenty"
,
"Thirty"
,
"Forty"
,
"Fifty"
,
"Sixty"
,
"Seventy"
,
"Eighty"
,
"Ninety"
};
if
(n < 20L)
{
return
(first_twenty[(
int
)n]);
}
var
answer =
""
;
for
(
long
i = n; i > 0;
i %= limit,
limit /= 1000)
{
curr_hun = i / limit;
while
(curr_hun == 0)
{
i %= limit;
limit /= 1000;
curr_hun = i / limit;
++t;
}
if
(curr_hun > 99)
{
answer += (first_twenty[(
int
)((
int
)curr_hun / 100)] +
" Hundred "
);
}
curr_hun = curr_hun % 100;
if
(curr_hun > 0 && curr_hun < 20)
{
answer += (first_twenty[(
int
)curr_hun] +
" "
);
}
else
if
(curr_hun % 10 == 0 && curr_hun != 0)
{
answer += (tens[(
int
)((
int
)curr_hun / 10) - 1] +
" "
);
}
else
if
(curr_hun > 20 && curr_hun < 100)
{
answer += (tens[(
int
)((
int
)curr_hun / 10) - 1] +
" "
+ first_twenty[(
int
)curr_hun % 10] +
" "
);
}
if
(t < 4)
{
answer += (multiplier[(
int
)++t] +
" "
);
}
}
return
(answer);
}
public
static
void
Main(String[] args)
{
var
n = 36L;
Console.WriteLine(numtowords.numberToWords(n));
n = 123456789;
Console.WriteLine(numtowords.numberToWords(n));
n = 10101010110001L;
Console.WriteLine(numtowords.numberToWords(n));
n = 999999999;
Console.WriteLine(numtowords.numberToWords(n));
}
}
Javascript
function
numberToWords(n)
{
let limit = 1000000000000, t = 0
if
(n == 0)
{
console.log(
"zero"
)
return
}
let multiplier = [
""
,
"Trillion"
,
"Billion"
,
"Million"
,
"Thousand"
]
let first_twenty = [
""
,
"One"
,
"Two"
,
"Three"
,
"Four"
,
"Five"
,
"Six"
,
"Seven"
,
"Eight"
,
"Nine"
,
"Ten"
,
"Eleven"
,
"Twelve"
,
"Thirteen"
,
"Fourteen"
,
"Fifteen"
,
"Sixteen"
,
"Seventeen"
,
"Eighteen"
,
"Nineteen"
]
let tens = [
""
,
"Twenty"
,
"Thirty"
,
"Forty"
,
"Fifty"
,
"Sixty"
,
"Seventy"
,
"Eighty"
,
"Ninety"
]
if
(n < 20)
{
console.log(first_twenty[n])
return
}
let answer =
""
let i = n
while
(i > 0)
{
let curr_hun = Math.floor(i / limit)
while
(curr_hun == 0)
{
i %= limit
limit /= 1000
curr_hun = Math.floor(i / limit)
t += 1
}
let flr = Math.floor(curr_hun / 100);
if
(curr_hun > 99)
answer += (first_twenty[flr] +
" tensundred "
)
curr_hun = curr_hun % 100
if
(curr_hun > 0 && curr_hun < 20)
answer += (first_twenty[curr_hun] +
" "
)
else
if
(curr_hun % 10 == 0 && curr_hun != 0){
flr = Math.floor(curr_hun / 10);
answer += (tens[flr - 1] +
" "
)
}
else
if
(curr_hun > 19 && curr_hun < 100){
flr = Math.floor(curr_hun / 10);
answer += (tens[flr - 1] +
" "
+
first_twenty[curr_hun % 10] +
" "
)
}
if
(t < 4)
answer += (multiplier[t] +
" "
)
i = i % limit
limit = Math.floor(limit / 1000)
}
console.log(answer)
}
let n = 36
numberToWords(n)
n = 123456789
numberToWords(n)
n = 10101010110001
numberToWords(n)
n = 999999999
numberToWords(n)
Output
Thirty Six One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Eighty Nine Ten Trillion One Hundred One Billion Ten Million One Hundred Ten Thousand One Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
Time Complexity: O(Log10(N))
Auxiliary Space: O(1)