ISC Computer Science Practical 2011
Write a program to input a natural number less than 1000 and display it in words.
Test your program on the sample data and some random data.
Sample input and output of the program.Input: 29
Output: TWENTY NINE
Input: 17001
Output: OUT OF RANGE
Input: 119
Output: ONE HUNDRED AND NINETEEN
Input: 500
Output: FIVE HUNDRED
import java.util.*;
public class NumberWords {
public static void main(String[] args) {
ToWords numbObject = new ToWords();
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Input:");
int i = in.nextInt();
if (i >= 1000) {
System.out.println("Output: OUT OF RANGE");
continue;
}
numbObject.toWord(i);
System.out.println("Want to input another number? Y/N:");
String c = in.next();
if(c.equals("y") || c.equals("Y"))
continue;
else
break;
}
in.close();
}
}
class ToWords {
public void toWord(int numb) {
this.number = numb;
this.word = "";
String temp = "";
temp +=number;
switch(temp.length()) {
case 1:
word = oneTwoDigit[number-1];
break;
case 2:
if ( number <= 20)
word = oneTwoDigit[number-1];
else {
if ( (temp.charAt(1)-48) == 0 )
word += tens[(temp.charAt(0)-48)-1];
else
word += tens[(temp.charAt(0)-48)-1]+ " "+oneTwoDigit[(temp.charAt(1)-48)-1];
}
break;
case 3:
if ( (temp.charAt(1)-48) == 0 && (temp.charAt(2)-48) == 0)
word += hundreds[(temp.charAt(0)-48)-1];
else if ( (temp.charAt(2)-48) == 0 )
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+tens[(temp.charAt(1)-48)-1];
else {
if ( (temp.charAt(1)-48) == 0 )
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+oneTwoDigit[(temp.charAt(2)-48)-1];
else
if ( (temp.charAt(1)-48) == 1 )
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+oneTwoDigit[(temp.charAt(2)-48)-1+10];
else
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+tens[(temp.charAt(1)-48)-1]+"-"+oneTwoDigit[(temp.charAt(2)-48)-1];
}
break;
case 4:
word = "One Thousand";
break;
}
System.out.println(number+" in words: "+word);
}
private String[] oneTwoDigit = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN",
"EIGHT","NINE","TEN","ELEVEN","TWELVE",
"THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",
"SEVENTEEN","EIGHTEEN","NINETEEN","TWENTY"};
private String[] tens = {"TEN","TWENTY","THIRTY","FORTY","FIFTY","SIXTY",
"SEVENTY","EIGHTY","NINETY"};
private String[] hundreds = {"ONE HUNDRED","TWO HUNDRED","THREE HUNDRED","FOUR HUNDRED","FIVE HUNDRED",
"SIX HUNDRED","SEVEN HUNDRED","EIGHT HUNDRED","NINE HUNDRED"};
private int number;
private String word;
}
Write a program to input a natural number less than 1000 and display it in words.
Test your program on the sample data and some random data.
Sample input and output of the program.Input: 29
Output: TWENTY NINE
Input: 17001
Output: OUT OF RANGE
Input: 119
Output: ONE HUNDRED AND NINETEEN
Input: 500
Output: FIVE HUNDRED
import java.util.*;
public class NumberWords {
public static void main(String[] args) {
ToWords numbObject = new ToWords();
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Input:");
int i = in.nextInt();
if (i >= 1000) {
System.out.println("Output: OUT OF RANGE");
continue;
}
numbObject.toWord(i);
System.out.println("Want to input another number? Y/N:");
String c = in.next();
if(c.equals("y") || c.equals("Y"))
continue;
else
break;
}
in.close();
}
}
class ToWords {
public void toWord(int numb) {
this.number = numb;
this.word = "";
String temp = "";
temp +=number;
switch(temp.length()) {
case 1:
word = oneTwoDigit[number-1];
break;
case 2:
if ( number <= 20)
word = oneTwoDigit[number-1];
else {
if ( (temp.charAt(1)-48) == 0 )
word += tens[(temp.charAt(0)-48)-1];
else
word += tens[(temp.charAt(0)-48)-1]+ " "+oneTwoDigit[(temp.charAt(1)-48)-1];
}
break;
case 3:
if ( (temp.charAt(1)-48) == 0 && (temp.charAt(2)-48) == 0)
word += hundreds[(temp.charAt(0)-48)-1];
else if ( (temp.charAt(2)-48) == 0 )
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+tens[(temp.charAt(1)-48)-1];
else {
if ( (temp.charAt(1)-48) == 0 )
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+oneTwoDigit[(temp.charAt(2)-48)-1];
else
if ( (temp.charAt(1)-48) == 1 )
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+oneTwoDigit[(temp.charAt(2)-48)-1+10];
else
word += hundreds[(temp.charAt(0)-48)-1]+" AND "+tens[(temp.charAt(1)-48)-1]+"-"+oneTwoDigit[(temp.charAt(2)-48)-1];
}
break;
case 4:
word = "One Thousand";
break;
}
System.out.println(number+" in words: "+word);
}
private String[] oneTwoDigit = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN",
"EIGHT","NINE","TEN","ELEVEN","TWELVE",
"THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",
"SEVENTEEN","EIGHTEEN","NINETEEN","TWENTY"};
private String[] tens = {"TEN","TWENTY","THIRTY","FORTY","FIFTY","SIXTY",
"SEVENTY","EIGHTY","NINETY"};
private String[] hundreds = {"ONE HUNDRED","TWO HUNDRED","THREE HUNDRED","FOUR HUNDRED","FIVE HUNDRED",
"SIX HUNDRED","SEVEN HUNDRED","EIGHT HUNDRED","NINE HUNDRED"};
private int number;
private String word;
}
No comments:
Post a Comment