Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Number to Words

Updated 21 Oct 2026

This program reads a whole number and prints it in English words, for example 4056 as FOUR THOUSAND FIFTY SIX. This is the Main version: everything is written in the main method, which splits the number into thousands and the last three digits and builds the words in the string result.

The program works for 0 to 9999 and prints INVALID INPUT for a negative number or a number above 9999. The words are in UPPER CASE and the word "AND" is not used (110 is ONE HUNDRED TEN).

Definition

Writing a number in words means reading its digits by place value: thousands, hundreds, then the last two digits. Numbers from 1 to 19 each have their own word, and from 20 onwards the tens word is followed by the units word.

  • $56$ is the tens word for 5 (FIFTY) and the units word for 6 (SIX): FIFTY SIX.
  • $4056$ is the thousands part FOUR THOUSAND followed by the words for the last three digits, 056, which is FIFTY SIX. The hundreds digit is 0, so no hundreds word is printed.

Two arrays hold all the words needed: ones for 0 to 19 and tens for 20, 30, ... 90.

Algorithm

  1. Read the number n. If it is below 0 or above 9999, print INVALID INPUT and stop.
  2. Store the words for 0 to 19 in the array ones (the index is the number itself) and the words for 20, 30, ... 90 in the array tens (the index is the tens digit minus 2).
  3. If n is 0, the result is ZERO.
  4. Otherwise split the number: th = n / 1000 is the thousands digit and rest = n % 1000 is the last three digits.
  5. If th > 0, start the result with ones[th] + " THOUSAND", followed by a space if rest is not 0.
  6. If rest is not 0, put x = rest into words:
    • if x >= 100, add ones[x / 100] + " HUNDRED", keep the last two digits with x = x % 100, and add a space if x is not 0;
    • for the part below 100: if x < 20 add ones[x]; otherwise add the tens word tens[x / 10 - 2] and, only if the units digit is not 0, a space and ones[x % 10].
  7. Print the number followed by in words: and the result.

Program

Everything is written inside main: the two word arrays, the split into thousands and the last three digits, and the hundreds, tens and units steps.

import java.util.Scanner;

/* Program NumberToWords reads a whole number from 0 to 9999 and prints it in
   UPPER CASE words without the word "AND" (4056 -> FOUR THOUSAND FIFTY SIX). */
public class NumberToWords
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number (0 to 9999): ");
        int n = sc.nextInt();                  // the number typed by the user
        if (n < 0 || n > 9999)
        {
            System.out.println("INVALID INPUT");
        }
        else
        {
            String ones[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
                             "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN",
                             "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
                             "NINETEEN"};      // words for 0 to 19 (index = the number itself)
            String tens[] = {"TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY",
                             "EIGHTY", "NINETY"};   // words for 20, 30, ... 90 (index = digit - 2)
            String result = "";                // the number in words, built by concatenation
            if (n == 0)
            {
                result = ones[0];
            }
            else
            {
                int th = n / 1000;             // thousands digit (0 to 9)
                int rest = n % 1000;           // last three digits
                if (th > 0)
                {
                    result = ones[th] + " THOUSAND";
                    if (rest != 0)
                        result = result + " ";
                }
                if (rest != 0)
                {
                    int x = rest;              // the number below 1000 that is being put into words
                    if (x >= 100)
                    {
                        result = result + ones[x / 100] + " HUNDRED";   // hundreds digit
                        x = x % 100;           // keep the last two digits
                        if (x != 0)
                            result = result + " ";
                    }
                    if (x != 0)                // the part below 100
                    {
                        if (x < 20)
                            result = result + ones[x];          // 1 to 19 come straight from the array
                        else
                        {
                            result = result + tens[x / 10 - 2]; // tens digit decides the tens word
                            if (x % 10 != 0)
                                result = result + " " + ones[x % 10];   // units word only if the units digit is not 0
                        }
                    }
                }
            }
            System.out.println(n + " in words: " + result);
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
nintThe number typed by the user
onesString[]Words for 0 to 19, the index is the number itself
tensString[]Words for 20, 30, ... 90, the index is the tens digit minus 2
resultStringThe number in words, built by concatenation
thintThousands digit (0 to 9)
restintThe last three digits of n
xintThe number below 1000 being put into words, reduced to its last two digits after the hundreds are done

Sample input and output

Sample 1

Enter a number (0 to 9999): 4056
4056 in words: FOUR THOUSAND FIFTY SIX

Sample 2

Enter a number (0 to 9999): 0
0 in words: ZERO

Sample 3

Enter a number (0 to 9999): 110
110 in words: ONE HUNDRED TEN

Sample 4

Enter a number (0 to 9999): 1000
1000 in words: ONE THOUSAND

Sample 5

Enter a number (0 to 9999): 13
13 in words: THIRTEEN

Sample 6

Enter a number (0 to 9999): 10000
INVALID INPUT

Sample 7

Enter a number (0 to 9999): -5
INVALID INPUT

One value per line

Dry run

Take the input $n = 4056$. The number is valid, then th = 4056 / 1000 = 4 and rest = 4056 \% 1000 = 56.

StepStatementValue
1th = n / 10004
2rest = n % 100056
3result = ones[th] + " THOUSAND"FOUR THOUSAND
4rest != 0, so a space is addedFOUR THOUSAND
5x = rest = 56: x < 100, so there is no hundreds part-
6x >= 20, so the tens word is tens[56 / 10 - 2] = tens[3]FOUR THOUSAND FIFTY
756 % 10 = 6, not 0, so " " + ones[6] is addedFOUR THOUSAND FIFTY SIX

The program prints 4056 in words: FOUR THOUSAND FIFTY SIX, the same as the first sample run.

Variations

  1. Up to 999 "Write a program to accept a number from 1 to 999 and display it in words."
  2. With AND "Display the number in words with the word AND, for example ONE HUNDRED AND TEN."
  3. Larger range "Accept a number up to 99999 and print it in words."