Google News
logo
Java Program to Convert Number to Word
The following example of Java program to convert a given number to its equivalent word representation :
Program :
import java.util.Scanner;

public class NumberToWordConverter {
    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"
    };
    private static final String[] thousands = {"", "thousand", "million", "billion"};

    private static String convert(int n) {
        if (n < 20) {
            return ones[n];
        }
        if (n < 100) {
            return tens[n / 10] + (n % 10 != 0 ? " " + ones[n % 10] : "");
        }
        if (n < 1000) {
            return ones[n / 100] + " hundred" + (n % 100 != 0 ? " " + convert(n % 100) : "");
        }
        for (int i = 3; i >= 0; i--) {
            if (n >= Math.pow(1000, i)) {
                return convert(n / (int) Math.pow(1000, i)) + " " + thousands[i] + (n % Math.pow(1000, i) != 0 ? " " + convert(n % (int) Math.pow(1000, i)) : "");
            }
        }
        return "";
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = scanner.nextInt();
        System.out.println(convert(n));
    }
}
Output :
Enter a number: 369
three hundred sixty nine

In this program, we have used a recursive approach to convert a given number to its equivalent word representation.

We have defined three arrays - ones, tens, and thousands to store the words for numbers up to 19, tens from 20 to 90, and thousand powers respectively.

Then, we have defined a recursive function convert to convert the given number to its equivalent word representation.