Hi Ranchers, I recently came through this !! Can anyone post the code for this? Here is the question :- Get an input integer (from 0 to 99999). The program should print the english equivalent for that integer. for example, 17540 should get printed as seventeen thousand five hundred and forty.

> NO LIBRARY FUNCTIONS(FOR THIS LOGIC) SHOULD BE USED > NO SQL STATEMENTS SHOULD BE USED.

Ok, as Satou already said, the forums here are not for having your homework done for you...

To solve this, first think about the problem yourself. How would you program something like this? Just think about the steps to solve the problem, without being concerned how it has to be put in Java code.

Here are some ideas to give you a start:

- You'll need a way to convert digits 0, 1, 2, ... to words: zero, one, two, ... - You'll have to look at the tenthousands, thousands, hundreds, etc. of the number. - Make a program that looks at all the digits of the number from left to right and convert them to text, digit by digit.

I would suggest that you back up even further than Jesper says and figure out how YOU do this. When you see a number like 173, how do you go from the numerical representation to the words "one hundred seventy three"? Pick some other examples to help you understand how you convert numbers into words. Can you describe the steps you use in English or your native language?

Layne

p.s. Please use a more descriptive subject. Certainly there is someone that can solve this problem. However, a subject like "converting numbers into words" would make the intent of your post more clear.

// // so quintillions is as big as it gets. The // program would automatically handle larger // numbers if this array were extended. // private static final String[] groups = { "", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion" };

private String string = new String();

public String getString() { return string; }

public String EnglishNumber(long n) {

// Go through the number one group at a time.

for (int i = groups.length - 1; i >= 0; i--) {

// Is the number as big as this group?

long cutoff = (long) Math.pow((double) 10, (double) (i * 3));

if (n >= cutoff) { int thisPart = (int) (n / cutoff);

// Use the ones[] array for both the // hundreds and the ones digit. Note // that tens[] starts at "twenty".

// // so quintillions is as big as it gets. The // program would automatically handle larger // numbers if this array were extended. // private static final String[] groups = { "", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion" };

private String string = new String();

public String getString() { return string; }

public String EnglishNumber(long n) {

// Go through the number one group at a time. System.out.println("groups.length= " + groups.length); for (int i = groups.length - 1; i >= 0; i--) {

// Is the number as big as this group?

long cutoff = (long) Math.pow((double) 10, (double) (i * 3));

if (n >= cutoff) { int thisPart = (int) (n / cutoff);

// Use the ones[] array for both the // hundreds and the ones digit. Note // that tens[] starts at "twenty".

Maybe you can draw some inspiration from this guy, apparently he's done it for a number of different languages and number formats. [ January 19, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

Mike, I've seen that kind of brute force approach used to great effect.

A friend wrote a date manipulation routine this way. In COBOL it's easy to take part of a string or number as another number, like pulling 0119 out of 20060119. He hand typed a table with 1231 entries. To see if 0119 is valid, just pull the entry at that index. Entry 119 says it's a valid day, also has the day of the month, quarter and year in it. The only special case is 0229 and for that you check if YYYY is a leap year. By getting entry 2006 from the leap year table of course. The program had about 8,000 lines of data and 400 lines of code for a couple dozen different functions.

Measuring quality depends on your priorities. His top priority was the shortest possible execution path and it was excellent by that measure.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108

posted

0

Yeah, I have too. Sometimes that's exactly what's called for. Isn't that the essence of lookup tables? Most of the time we could code the logic that creates the table but it's much faster to precompute and lookup the cached answer.

Of course in the case of a homework assignment the brute force approach isn't going to score too well in the design category.