Wednesday , 31 August 2016
Home » Class 12 » ISC 2011 [Question 1] Practical Paper Solved [Number in Words]

ISC 2011 [Question 1] Practical Paper Solved [Number in Words]

Question:

Write a program to input a natural number less than 1000 and display it in words. [Note we have solved the program for numbers in the range [1-9999]

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

Sample Data:

Input: 29
Output: TWENTY NINE

Input: 17001
Output: OUT OF RANGE

Input: 119
Output: ONE HUNDRED AND NINETEEN

Input: 500
Output: FIVE HUNDRED

Programming Code:

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

import java.io.*;
class Num2Word_ISC2011
{
    public static void main(String args[]) throws IOException
       {
        BufferedReader br=new BufferedReader(new InputStreamReader(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=Integer.parseInt(br.readLine());
        
        /*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]);
        }
    }
}

Output:

1. Enter a Number : 129
Output = One Hundred And Twenty Nine

2. Enter a Number : 8307
Output = Eight Thousand Three Hundred And Seven

3. Enter a Number : 54987
Out of Range

Check Also

default thumbnail

Java Program to Sort an Array using Bubble Sort Algorithm

Java program to input an array of integers and sort it using Bubble Sorting Algorithm.

2 comments

  1. your response was quick!! 🙂

     

  2. thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *