Program to Convert Digit/Number to Words in Java

Converting Numbers to Words

Converting a number to words is a common problem in programming.

We will explore three different methods to achieve this in Java.

Method 1: Using Arrays

This method utilizes an array to map digits to their word equivalents.

import java.util.Scanner;

public class NumberToWords {
    static String[] words = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    
    public static void convertToWords(int num) {
        if (num >= 0 && num <= 9)
            System.out.println(words[num]);
        else
            System.out.println("Invalid input!");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a single-digit number: ");
        int num = scanner.nextInt();
        convertToWords(num);
        scanner.close();
    }
}
            
Input: 5
Output: Five

Method 2: Using String Manipulation

This method handles multi-digit numbers using iteration.

import java.util.Scanner;

public class NumberToWords {
    static String[] words = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    
    public static void convertToWords(int num) {
        String numStr = Integer.toString(num);
        for (char digit : numStr.toCharArray()) {
            System.out.print(words[digit - '0'] + " ");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        convertToWords(num);
        scanner.close();
    }
}
            
Input: 123
Output: One Two Three

Method 3: Using Recursion

This method recursively processes each digit.

import java.util.Scanner;

public class NumberToWords {
    static String[] words = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    
    public static void convertToWordsRecursive(int num) {
        if (num == 0)
            return;
        convertToWordsRecursive(num / 10);
        System.out.print(words[num % 10] + " ");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        if (num == 0)
            System.out.println("Zero");
        else
            convertToWordsRecursive(num);
        scanner.close();
    }
}
            
Input: 507
Output: Five Zero Seven
Numbers

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

HCF - Highest Common Factor: C C++ Java Python

LCM - Lowest Common Multiple: C C++ Java Python

GCD - Greatest Common Divisor: C C++ Java Python

Binary to Decimal Conversion: C C++ Java Python

Octal to Decimal Conversion: C C++ Java Python

Hexadecimal to Decimal Conversion: C C++ Java Python

Decimal to Binary Conversion: C C++ Java Python

Decimal to Octal Conversion: C C++ Java Python

Decimal to Hexadecimal Conversion: C C++ Java Python

Binary to Octal Conversion: C C++ Java Python

Quadrants in which a given coordinate lies: C C++ Java Python

Addition of Two Fractions: C C++ Java Python

Calculate the Area of a Circle: C C++ Java Python

Convert Digit/Number to Words: C C++ Java Python

Finding Roots of a Quadratic Equation: C C++ Java Python