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
- Read the number
n. If it is below 0 or above 9999, printINVALID INPUTand stop. - 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 arraytens(the index is the tens digit minus 2). - If
nis 0, the result isZERO. - Otherwise split the number:
th = n / 1000is the thousands digit andrest = n % 1000is the last three digits. - If
th > 0, start the result withones[th] + " THOUSAND", followed by a space ifrestis not 0. - If
restis not 0, putx = restinto words:- if
x >= 100, addones[x / 100] + " HUNDRED", keep the last two digits withx = x % 100, and add a space ifxis not 0; - for the part below 100: if
x < 20addones[x]; otherwise add the tens wordtens[x / 10 - 2]and, only if the units digit is not 0, a space andones[x % 10].
- if
- 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
n | int | The number typed by the user |
ones | String[] | Words for 0 to 19, the index is the number itself |
tens | String[] | Words for 20, 30, ... 90, the index is the tens digit minus 2 |
result | String | The number in words, built by concatenation |
th | int | Thousands digit (0 to 9) |
rest | int | The last three digits of n |
x | int | The 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 SIXSample 2
Enter a number (0 to 9999): 0
0 in words: ZEROSample 3
Enter a number (0 to 9999): 110
110 in words: ONE HUNDRED TENSample 4
Enter a number (0 to 9999): 1000
1000 in words: ONE THOUSANDSample 5
Enter a number (0 to 9999): 13
13 in words: THIRTEENSample 6
Enter a number (0 to 9999): 10000
INVALID INPUTSample 7
Enter a number (0 to 9999): -5
INVALID INPUTDry run
Take the input $n = 4056$. The number is valid, then th = 4056 / 1000 = 4 and rest = 4056 \% 1000 = 56.
| Step | Statement | Value |
|---|---|---|
| 1 | th = n / 1000 | 4 |
| 2 | rest = n % 1000 | 56 |
| 3 | result = ones[th] + " THOUSAND" | FOUR THOUSAND |
| 4 | rest != 0, so a space is added | FOUR THOUSAND |
| 5 | x = rest = 56: x < 100, so there is no hundreds part | - |
| 6 | x >= 20, so the tens word is tens[56 / 10 - 2] = tens[3] | FOUR THOUSAND FIFTY |
| 7 | 56 % 10 = 6, not 0, so " " + ones[6] is added | FOUR THOUSAND FIFTY SIX |
The program prints 4056 in words: FOUR THOUSAND FIFTY SIX, the same as the first sample run.
Variations
- Up to 999 "Write a program to accept a number from 1 to 999 and display it in words."
- With AND "Display the number in words with the word AND, for example ONE HUNDRED AND TEN."
- Larger range "Accept a number up to 99999 and print it in words."