Java String Program · S63
Convert an Integer to English Words in Java
Turn 2000010 into "Two Million Ten". Split the number into groups of three digits, name each group, and attach Thousand, Million or Billion.
Problem Statement
Given a non-negative int, return its English name, with each word capitalised and separated by single spaces, for example "One Hundred Five".
Zero is "Zero". Groups that are zero are left out, so 2000010 is "Two Million Ten", with no "Thousand".
Input/Output Examples
105Output"One Hundred Five"Explanation: no "and" and no "Zero" for the missing tens.2000010Output"Two Million Ten"Explanation: the thousands group is 000, so it is skipped.1234567891Output"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"Explanation: four groups: 1, 234, 567 and 891.Visual Explanation
- Splitgroups2000010by thousands, from the right
- Name group 0wordsTen010 → Ten
- Skip group 1words—000 adds nothing
- Name group 2wordsTwo Million2 + Million
- Done"Two Million Ten"
Every group below 1000 is spelled the same way, whatever its scale. That reduces the problem to one helper for 0–999 plus a list of scale words.
Approaches
Both approaches use the same word tables. The first loops over groups of three digits; the second recurses on the largest scale.
Approach 1: Three-Digit Groups (Recommended)
Idea: take num % 1000 repeatedly, spell each non-zero group and prepend it with its scale word.
- Return "Zero" for 0.
- While
num > 0, spellnum % 1000with the helper and add Thousand, Million or Billion. - Insert the words at the front, then divide
numby 1000. - The helper handles hundreds, then tens, then 1–19.
Time: O(1) · Space: O(1). An int has at most four groups.
Approach 2: Recursive Decomposition
Idea: name the largest part of the number (billions, millions, thousands, hundreds, tens) and recurse on the remainder.
- Below 20, look the word up directly.
- Below 100, use the tens word plus the words for the remainder.
- For larger numbers, recurse on
n / scale, add the scale word, then recurse onn % scale. - Trim spaces left by empty remainders.
Time: O(1) · Space: O(1). The recursion depth is bounded by the number of scales.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Three-Digit Groups (Recommended)
Single spaces are added only between non-empty parts, so no trimming is needed.
public class NumberWordsChunks {
private static final String[] BELOW_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"};
private static final String[] TENS = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
private static final String[] SCALES = {"", "Thousand", "Million", "Billion"};
// Splits the number into groups of three digits and names each group with its scale
static String toWords(int num) {
if (num == 0) {
return "Zero";
}
StringBuilder result = new StringBuilder();
int scale = 0;
while (num > 0) {
int chunk = num % 1000;
if (chunk != 0) {
String words = belowThousand(chunk) + (scale > 0 ? " " + SCALES[scale] : "");
result.insert(0, result.length() > 0 ? words + " " : words);
}
num /= 1000;
scale++;
}
return result.toString();
}
private static String belowThousand(int n) {
StringBuilder sb = new StringBuilder();
if (n >= 100) {
sb.append(BELOW_20[n / 100]).append(" Hundred");
n %= 100;
}
if (n >= 20) {
sb.append(sb.length() > 0 ? " " : "").append(TENS[n / 10]);
n %= 10;
}
if (n > 0) {
sb.append(sb.length() > 0 ? " " : "").append(BELOW_20[n]);
}
return sb.toString();
}
public static void main(String[] args) {
int[] inputs = {0, 105, 2000010, 1234567891, Integer.MAX_VALUE};
for (int n : inputs) {
System.out.println(n + " -> \"" + toWords(n) + "\"");
}
}
}
0 -> "Zero" 105 -> "One Hundred Five" 2000010 -> "Two Million Ten" 1234567891 -> "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One" 2147483647 -> "Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven"
Time complexity: O(1) · Space complexity: O(1).
Approach 2: Recursive Decomposition
Mirrors how people read numbers aloud, which makes each branch easy to check.
public class NumberWordsRecursive {
private static final String[] BELOW_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"};
private static final String[] TENS = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
static String toWords(int num) {
return num == 0 ? "Zero" : words(num).trim();
}
// Each call names the largest part and recurses on the remainder
private static String words(int n) {
if (n == 0) {
return "";
} else if (n < 20) {
return BELOW_20[n];
} else if (n < 100) {
return (TENS[n / 10] + " " + words(n % 10)).trim();
} else if (n < 1_000) {
return (BELOW_20[n / 100] + " Hundred " + words(n % 100)).trim();
} else if (n < 1_000_000) {
return (words(n / 1_000) + " Thousand " + words(n % 1_000)).trim();
} else if (n < 1_000_000_000) {
return (words(n / 1_000_000) + " Million " + words(n % 1_000_000)).trim();
}
return (words(n / 1_000_000_000) + " Billion " + words(n % 1_000_000_000)).trim();
}
public static void main(String[] args) {
int[] inputs = {0, 105, 2000010, 1234567891, Integer.MAX_VALUE};
for (int n : inputs) {
System.out.println(n + " -> \"" + toWords(n) + "\"");
}
}
}
0 -> "Zero" 105 -> "One Hundred Five" 2000010 -> "Two Million Ten" 1234567891 -> "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One" 2147483647 -> "Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven"
Time complexity: O(1) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Three-Digit Groups | O(1) | O(1) |
| Recursive Decomposition | O(1) | O(1) |
An int has at most 10 digits, so both approaches do a bounded amount of work. Expressed in terms of the number of digits d, the work is O(d).
Interview Tips and Recommended Approach
- Handle zero separately. Every other case avoids printing "Zero" inside a number.
- Skip empty groups. 1000000 is "One Million", not "One Million Zero Thousand".
- Watch the spaces. Many bugs come from double spaces when a part is empty, as in 1000001.
- Ask about the format. British English adds "and" ("One Hundred and Five"); confirm which style is expected.
Recommended Approach: Three-Digit Groups
Why: it applies one small helper to each group of three digits, which keeps the special cases in a single place and avoids trimming.