ISC practical papers › ISC 2011 › Question 1
Number to words
ISC 2011 Computer Science Practical, Question 1 · Number programs, Strings
The question
Write a program to input a natural number less than 1000 and output it in words. Test your program for the following set of data:
INPUT: 29 OUTPUT: TWENTY NINE
INPUT: 17001 OUTPUT: OUT OF RANGE
INPUT: 119 OUTPUT: ONE HUNDRED NINETEEN
INPUT: 500 OUTPUT: FIVE HUNDRED
Question: ISC 2011 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class Number {
private int n;
private String answer;
public Number(int n) {
this.n = n;
answer = "";
convertToWords();
}
public void convertToWords() {
String arr1[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN",
"TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" };
String arr2[] = { "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHT", "NINETY" };
int factor[] = { 1000000000, 10000000, 100000, 1000, 100, 1 };
String units[] = { "ARAB", "CRORE", "LAKHS", "THOUSAND", "HUNDRED", "" };
int quotient, number = n, ones, tens;
for (int i = 0; i < factor.length; i++) {
quotient = number / factor[i];
if (quotient > 0) {
if (quotient < 20) {
answer = answer + arr1[quotient - 1] + " ";
} else {
ones = quotient % 10;
tens = quotient / 10;
if (ones != 0) {
answer += arr2[tens - 2] + " " + arr1[ones - 1] + " ";
} else {
answer += arr2[tens - 2] + " ";
}
}
answer += units[i] + " ";
}
number = number % factor[i];
if (number == 0)
break;
}
}
public String toString() {
return answer.trim();
}
}
class ISC2011Q1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\t");
int n = in.nextInt();
if (n > 0 && n < 1000) {
Number obj = new Number(n);
System.out.println("OUTPUT:\t" + obj);
} else {
System.out.println("OUTPUT: OUT OF RANGE");
}
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.