Java Number Program · OT42
Convert a Number to Indian Currency Words in Java
The Indian system groups digits differently: after the last three digits, every two digits form the next unit, so 1,50,075 is one lakh, fifty thousand and seventy-five.
Problem Statement
Given a non-negative whole number of rupees, return its name in English words using the Indian numbering system: thousand, lakh (1,00,000) and crore (1,00,00,000).
Write "Rupee" for exactly one rupee and "Rupees" otherwise. Zero is "Zero Rupees".
Input/Output Examples
150075OutputOne Lakh Fifty Thousand Seventy Five RupeesExplanation: 1,50,075 in Indian grouping.2540319OutputTwenty Five Lakh Forty Thousand Three Hundred Nineteen RupeesExplanation: 25,40,319.1500000000OutputOne Hundred Fifty Crore RupeesExplanation: 150 crore; the crore count itself is spelled out.Visual Explanation
- Group1,50,075150075lakh, thousand, hundred, rest
- NamewordsOne LakhFifty ThousandSeventy Fivehundreds part is 0: skipped
- Done… Rupees
Every Indian unit except the crore holds a value below 100, so one helper for 1–99 names almost everything. A crore count can be larger, so it is spelled with the same method recursively.
Approaches
Both approaches produce identical text. One divides by each unit in turn; the other recurses on the largest unit.
Approach 1: Divide by Each Unit (Recommended)
Idea: take the quotient for crore, lakh, thousand and hundred in turn, name each non-zero part, then name the last two digits.
- Return "Zero Rupees" for 0.
- For each unit, compute
part = rest / unitand name it if it is not 0. - Spell a crore part recursively, since it can exceed 99.
- Add the last two digits and "Rupee" or "Rupees".
Time: O(log n) · Space: O(log n).
Approach 2: Recursive Decomposition
Idea: name the largest unit that fits, then recurse on the quotient and the remainder.
- Below 20 and below 100, use the word tables.
- Below a thousand, a lakh and a crore, split on the matching unit.
- Join the parts with single spaces, skipping empty remainders.
Time: O(log n) · Space: O(log n).
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: Divide by Each Unit (Recommended)
Mirrors how amounts are read aloud in India, from the largest unit down.
public class RupeesChunks {
private static final String[] ONES = {"", "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"};
// Indian grouping: crore (10^7), lakh (10^5), thousand (10^3), hundred, then the last two digits
static String toRupees(long amount) {
if (amount == 0) {
return "Zero Rupees";
}
StringBuilder sb = new StringBuilder();
long[] units = {10_000_000L, 100_000L, 1_000L, 100L};
String[] names = {"Crore", "Lakh", "Thousand", "Hundred"};
long rest = amount;
for (int i = 0; i < units.length; i++) {
long part = rest / units[i];
if (part > 0) {
sb.append(i == 0 ? toRupeesWords(part) : belowHundred((int) part)).append(' ').append(names[i]).append(' ');
}
rest %= units[i];
}
sb.append(belowHundred((int) rest));
return sb.toString().trim() + (amount == 1 ? " Rupee" : " Rupees");
}
// A crore count can itself be large (for example 150 crore), so it is spelled recursively
private static String toRupeesWords(long n) {
String w = toRupees(n);
return w.substring(0, w.lastIndexOf(' '));
}
private static String belowHundred(int n) {
if (n < 20) {
return ONES[n];
}
return (TENS[n / 10] + " " + ONES[n % 10]).trim();
}
public static void main(String[] args) {
long[] inputs = {150075, 2540319, 10000000, 1500000000L, 1, 0};
for (long n : inputs) {
System.out.println(n + " -> " + toRupees(n));
}
}
}
150075 -> One Lakh Fifty Thousand Seventy Five Rupees 2540319 -> Twenty Five Lakh Forty Thousand Three Hundred Nineteen Rupees 10000000 -> One Crore Rupees 1500000000 -> One Hundred Fifty Crore Rupees 1 -> One Rupee 0 -> Zero Rupees
Time complexity: O(log n) · Space complexity: O(log n).
Approach 2: Recursive Decomposition
Each branch handles one unit, which keeps the special cases easy to find.
public class RupeesRecursive {
private static final String[] ONES = {"", "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 toRupees(long amount) {
if (amount == 0) {
return "Zero Rupees";
}
return words(amount) + (amount == 1 ? " Rupee" : " Rupees");
}
// Each call names the largest Indian unit and recurses on the quotient and the remainder
private static String words(long n) {
if (n == 0) {
return "";
}
if (n < 20) {
return ONES[(int) n];
}
if (n < 100) {
return join(TENS[(int) (n / 10)], ONES[(int) (n % 10)]);
}
if (n < 1_000) {
return join(words(n / 100) + " Hundred", words(n % 100));
}
if (n < 100_000) {
return join(words(n / 1_000) + " Thousand", words(n % 1_000));
}
if (n < 10_000_000) {
return join(words(n / 100_000) + " Lakh", words(n % 100_000));
}
return join(words(n / 10_000_000) + " Crore", words(n % 10_000_000));
}
private static String join(String a, String b) {
return b.isEmpty() ? a : a + " " + b;
}
public static void main(String[] args) {
long[] inputs = {150075, 2540319, 10000000, 1500000000L, 1, 0};
for (long n : inputs) {
System.out.println(n + " -> " + toRupees(n));
}
}
}
150075 -> One Lakh Fifty Thousand Seventy Five Rupees 2540319 -> Twenty Five Lakh Forty Thousand Three Hundred Nineteen Rupees 10000000 -> One Crore Rupees 1500000000 -> One Hundred Fifty Crore Rupees 1 -> One Rupee 0 -> Zero Rupees
Time complexity: O(log n) · Space complexity: O(log n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Divide by Each Unit | O(log n) | O(log n) |
| Recursive Decomposition | O(log n) | O(log n) |
Here n is the amount. The number of units, and so the amount of work, grows with the number of digits.
Interview Tips and Recommended Approach
- Know the grouping. 1,00,000 is a lakh and 1,00,00,000 is a crore; only the last group has three digits.
- Skip zero parts so 10000000 is "One Crore", not "One Crore Zero Lakh".
- Handle singular and plural for the currency word.
- Ask about paise. Decimal amounts need a second part, such as "and Fifty Paise".
Recommended Approach: Divide by Each Unit
Why: it follows the Indian reading order directly and keeps the unit table in one place, so it is easy to extend, for example with paise.