ISC Practicals and Theory - Number to Words (OOP)
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 OOP version: main reads the number and passes it to a parameterised constructor, and the methods isValid(), below100(), below1000() and words() each do one job and return their answer instead of printing it.
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 FOUR THOUSAND followed by the words for 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
mainreads the number intonumwith aScannerand creates the objectobwithnew NumberToWords(num). The constructor storesnuminnand fills the arraysonesandtens.maincallsob.isValid(), which returnstruewhennis from 0 to 9999.- If it returns
false,mainprintsINVALID INPUTand stops. - Otherwise
mainprintsnum, the textin words:and the String returned byob.words(). words():- if
nis 0 it returnsZERO; - the thousands digit is
th = n / 1000and the last three digits arerest = n % 1000; - if
this more than 0,wgetsones[th]and the word THOUSAND, and a space ifrestis not 0; - if
restis not 0, it adds the String returned bybelow1000(rest).
- if
below1000(x)returns the words for 1 to 999:- if
xis less than 100 it returnsbelow100(x); - otherwise it writes
ones[x / 100]and HUNDRED, and addsbelow100(r)for the last two digitsr = x % 100ifris not 0.
- if
below100(x)returns the words for 1 to 99:- if
xis less than 20 it returnsones[x]; - otherwise it takes
tens[x / 10 - 2](the tens digit minus 2 gives the array position) and addsones[x % 10]only if the units digit is not 0.
- if
Exam tip: A method that returns a value is easier to test than one that prints. Here words() only builds the String and main decides what to print.
Program
import java.util.Scanner;
/* Class NumberToWords: holds a number from 0 to 9999 and returns it in
UPPER CASE words without the word "AND" (4056 -> FOUR THOUSAND FIFTY SIX).
Every method does one job and returns its result; main only reads and prints. */
public class NumberToWords
{
int n; // the number to be converted
String ones[]; // words for 0 to 19 (index = the number itself)
String tens[]; // words for 20, 30, ... 90 (index 0 is TWENTY, so index = digit - 2)
// Parameterised constructor: stores num in n and fills both word arrays
// num : the number typed by the user
NumberToWords(int num)
{
n = num;
ones = new String[] {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN",
"FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
"NINETEEN"};
tens = new String[] {"TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY",
"EIGHTY", "NINETY"};
}
// Returns true if n is from 0 to 9999, otherwise false
boolean isValid()
{
return n >= 0 && n <= 9999;
}
// Returns the words for x, where x is from 1 to 99 (e.g. 56 -> "FIFTY SIX")
// x : the number to be converted
String below100(int x)
{
if (x < 20)
return ones[x]; // 1 to 19 come straight from the array
String w = tens[x / 10 - 2]; // tens digit decides the tens word
if (x % 10 != 0)
w = w + " " + ones[x % 10]; // add the units word only if units digit is not 0
return w;
}
// Returns the words for x, where x is from 1 to 999 (e.g. 110 -> "ONE HUNDRED TEN")
// x : the number to be converted
String below1000(int x)
{
if (x < 100)
return below100(x);
String w = ones[x / 100] + " HUNDRED"; // hundreds digit
int r = x % 100; // last two digits
if (r != 0)
w = w + " " + below100(r);
return w;
}
// Returns the words for n (valid, 0 to 9999), including 0 and the thousands part
String words()
{
if (n == 0)
return ones[0];
int th = n / 1000; // thousands digit (0 to 9)
int rest = n % 1000; // last three digits
String w = "";
if (th > 0)
{
w = ones[th] + " THOUSAND";
if (rest != 0)
w = w + " ";
}
if (rest != 0)
w = w + below1000(rest);
return w;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the number
System.out.print("Enter a number (0 to 9999): ");
int num = sc.nextInt(); // number typed by the user
NumberToWords ob = new NumberToWords(num); // object of the class
if (ob.isValid())
System.out.println(num + " in words: " + ob.words());
else
System.out.println("INVALID INPUT");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member that stores the number to be converted |
ones | String[] | Data member holding the words for 0 to 19 |
tens | String[] | Data member holding the words for 20, 30, ... 90 |
num | int | Parameter of the constructor, and in main the number typed by the user |
x | int | Parameter of below100() and below1000(): the number to be converted |
w | String | Local variable in below100(), below1000() and words() that collects the words |
r | int | Last two digits of x in below1000(), that is x % 100 |
th | int | Thousands digit of n (0 to 9) |
rest | int | Last three digits of n, that is n % 1000 |
sc | Scanner | Reads the number typed by the user |
ob | NumberToWords | Object of the class created in main |
args | String[] | Command-line arguments of main (not used) |
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 $num = 4056$. The constructor sets n = 4056, isValid() returns true, and words() runs.
| Step | Statement | Value |
|---|---|---|
| 1 | th = n / 1000 | 4 |
| 2 | rest = n % 1000 | 56 |
| 3 | w = ones[th] + " THOUSAND" | FOUR THOUSAND |
| 4 | rest != 0, so a space is added | FOUR THOUSAND |
| 5 | below1000(56): x < 100, so it returns below100(56) | - |
| 6 | below100(56): x >= 20, w = tens[56 / 10 - 2] = tens[3] | FIFTY |
| 7 | 56 % 10 = 6, not 0, so w = w + " " + ones[6] | FIFTY SIX |
| 8 | w = w + "FIFTY SIX" in words() | FOUR THOUSAND FIFTY SIX |
main 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."