SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Number in Words Program in Java and Python

29 January 2014

Number in words program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

Write a program to input a natural number less than 1000 and display it in words.

Note: The solution here also supports numbers up to 9999.

Test your program for the given sample data and some random data.

INPUT: Enter a number: 29 OUTPUT: TWENTY NINE INPUT: Enter a number: 17001 OUTPUT: OUT OF RANGE INPUT: Enter a number: 119 OUTPUT: ONE HUNDRED AND NINETEEN INPUT: Enter a number: 500 OUTPUT: FIVE HUNDRED

Algorithm:

Step 1: Start.

Step 2: Accept the number n as input.

Step 3: If n is outside the allowed range, display OUT OF RANGE and stop.

Step 4: Initialize arrays for ones, teens and tens word values.

Step 5: If n contains a thousands digit, use n / 1000 as an array index, display that word with THOUSAND, and store n % 1000 back in n.

Step 6: If n contains a hundreds digit, use n / 100 as an array index, display that word with HUNDRED, and store n % 100 back in n.

Step 7: If the remaining value of n is greater than 0 after hundreds, display AND.

Step 8: If n is from 10 to 19, use n - 10 as the index of the teens array and display the word.

Step 9: If n is 20 or more, use n / 10 as the tens index, display it, and reduce n to n % 10.

Step 10: If n is from 1 to 9, use n as the ones index and display the final word.

Step 11: Stop.

Explanation:

The program converts a number into words by breaking it into place values. This is easier than trying to compare the whole number with many separate cases.

Arrays are used to store fixed word values such as ONE, TWO, TEN, ELEVEN and TWENTY. Once the digit or two-digit value is known, the required word can be taken from the correct array position.

The thousands and hundreds parts are handled first because they are higher place values. After printing the hundred part, the word AND is printed only when there is still a remaining tens or ones part.

The last two digits need special handling because numbers from 10 to 19 have unique names. For all other two-digit values, the tens word and the ones word are printed separately.

The program converts a number into words by breaking it into meaningful place groups. Instead of reading the number as a whole, it separates units, tens, hundreds and larger parts according to the format required by the question. Arrays of word equivalents are usually used for values such as one to nineteen and for multiples of ten. This avoids a long chain of conditions. The main challenge is handling zero positions correctly so that unnecessary words are not printed while still preserving correct place names like hundred or thousand.

Java Program:

Java
/**
* The class Num2Word_ISC2011 accepts a number in the range [1-9999] and prints it in words.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;
class Num2Word_ISC2011
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        String ty[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};

        String ten[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
            "Eighteen","Nineteen"};

        String unit[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};

        System.out.print("Enter a Number : ");
        int n=sc.nextInt();

        /*checking whether the number is in the range [1-9999] or not*/
        if(n<1 || n>9999)
        System.out.println("Out of Range");

        else
        {
            int th=n/1000; //finding the digit at thousand's place
            int h=(n/100)%10; //finding the digit at hundred's place
            int t=(n/10)%10; //finding the digit at ten's place
            int u=n%10; //finding the digit at unit's place

            System.out.print("Output = ");

            /*Condition for printing digit at thousand's place, is that it should not be zero*/
            if(th!=0)
            System.out.print(unit[th]+" Thousand");

            /*Condition for printing digit at hundred's place, is that it should not be zero*/
            if(h!=0)
            System.out.print(" "+unit[h]+" Hundred");

            /*Condition for printing the word "And"*/
            if((t!=0 || u!=0)&&(th!=0 || h!=0))
            System.out.print(" And");

            /*Condition for printing digit at ten's place*/
            if(t==1) //When digit at ten's place is 1, we have different words like Ten, Eleven etc.
            System.out.print(" "+ten[u+1]);

            else //if it is not 1 then we print the words following a normal pattern
            System.out.print(" "+ty[t]+" "+unit[u]);
        }
    }
}

Equivalent Python Program:

Python
# Read the text input and split or scan it according to the question requirement.
# Loops process each word/character and update counters or result strings.
# Display the final text result after sorting, checking or rearranging is complete.

ones = ["", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"]
teens = ["TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"]
tens = ["", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"]

n = int(input("Enter a number: "))

if n < 1 or n > 9999:
    print("OUT OF RANGE")
else:
    words = ""

    if n >= 1000:
        words = words + ones[n // 1000] + " THOUSAND "
        n = n % 1000

    if n >= 100:
        words = words + ones[n // 100] + " HUNDRED "
        n = n % 100
        if n > 0:
            words = words + "AND "

    if n >= 20:
        words = words + tens[n // 10] + " "
        n = n % 10
    elif n >= 10:
        words = words + teens[n - 10] + " "
        n = 0

    if n > 0:
        words = words + ones[n]

    print(words.strip())

Output:

INPUT: Enter a number: 29 OUTPUT: TWENTY NINE INPUT: Enter a number: 17001 OUTPUT: OUT OF RANGE INPUT: Enter a number: 119 OUTPUT: ONE HUNDRED AND NINETEEN INPUT: Enter a number: 500 OUTPUT: FIVE HUNDRED

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →