Saturday, November 12, 2011

BlueJ Program On Displaying A Number In Words – As A Sentence



This is a BlueJ program on displaying a number in words. If 234 is entered, the output will be Two Hundred Thirty Four. Here is the program.

import java.io.*;
class Array1
{
int no,i,n, h,t,u;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{
while(true)
{
 System.out.println("\nEnter the Number:");
 no=Integer.parseInt(br.readLine());
 if(no<1000)
 break;
}
System.out.print("\nIn words=");
h=no/100;
if(h!=0)
{

 switch(h)
 {
     case 1:
     System.out.print(" One Hundred");
     break;
     case 2:
     System.out.print(" Two Hundred");
     break;
     case 3:
     System.out.print(" Three Hundred");
     break;
     case 4:
     System.out.print(" Four Hundred");
     break;
     case 5:
     System.out.print(" Five Hundred");
     break;
     case 6:
     System.out.print(" Six Hundred");
     break;
     case 7:
     System.out.print(" Seven Hundred");
     break;
     case 8:
     System.out.print(" Eight Hundred");
     break;
     case 9:
     System.out.print(" Nine Hundred");
     break;
    }
    no=no%100;
}

t=no/10;
if(t==1)
{
switch(no)
 {
     case 11:
     System.out.print(" Eleven");
     break;
     case 12:
     System.out.print(" Twelve");
     break;
     case 13:
     System.out.print(" Thirteen");
     break;
     case 14:
     System.out.print(" Fourteen");
     break;
     case 15:
     System.out.print(" Fifteen");
     break;
     case 16:
     System.out.print(" Sixteen");
     break;
     case 17:
     System.out.print(" Seventeen");
     break;
     case 18:
     System.out.print(" Eighteen");
     break;
     case 19:
     System.out.print(" Nineteen");
     break;
    }
}
    else
    {
       switch(t)
 {
     case 2:
     System.out.print(" Twenty");
     break;
     case 3:
     System.out.print(" Thirty");
     break;
     case 4:
     System.out.print(" Fourty");
     break;
     case 5:
     System.out.print(" Fifty");
     break;
     case 6:
     System.out.print(" Sixty");
     break;
     case 7:
     System.out.print(" Seventy");
     break;
     case 8:
     System.out.print(" Eighty");
     break;
     case 9:
     System.out.print(" Ninety");
     break;
       
    }
}
u=no%10;
 switch(u)
 {
     case 1:
     System.out.print(" One");
     break;
     case 2:
     System.out.print(" Two");
     break;
     case 3:
     System.out.print(" Three");
     break;
     case 4:
     System.out.print(" Four");
     break;
     case 5:
     System.out.print(" Five");
     break;
     case 6:
     System.out.print(" Six");
     break;
     case 7:
     System.out.print(" Seven");
     break;
     case 8:
     System.out.print(" Eight");
     break;
     case 9:
     System.out.print(" Nine");
     break;
       
    }
}
public static void main(String args[]) throws Exception
{
 Array1 ob=new Array1();
 ob.take();
}
}


Technical analysis of the BlueJ program

Firstly the entered number is checked whether it is a four digit or more than four digit number. If so, number is re-entered.  The number may be of 2 digit. In case of three digit number, hundred position digit is displayed using the word ‘hundred’ after the equivalent word of the digit. Then the tenth position digit is checked and if it is found to be ‘1’, the last two digit number is displayed in equivalent word. In other case, tenth position digit is displayed in word like twenty, thirty and lastly the unit digit equivalent word is displayed.

 Sample input and output

Enter the Number:
267

In words= Two Hundred Sixty Seven

Enter the Number:
1234

Enter the Number:
670

In words= Six Hundred Seventy

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner