Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
• Junilu Lacar
Sheriffs:
• Tim Cooke
• Jeanne Boyarsky
• Knute Snortum
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Piet Souris
Bartenders:
• salvin francis
• fred rosenberger
• Frits Walraven

# Program to print numbers from 0 to 100 in words

Ranch Hand
Posts: 185
Please give me inputs about this program. Like is this a good program, does it follow all conventions. Can this program be done in less number of steps etc.
I am a novice in programming and is doing this so that i can become good in programming.

Marshal
Posts: 68976
275
Lots of people are given that exercise. I would suggest you divide your array into several arrays, putting "eleven" "twelve" . . . "nineteen" in one, and "ten" "twenty" . . . "ninety" in another. Using "" or even "\b" for the 0th element in each may make counting easier.

Sheriff
Posts: 21922
106
You'll also want to use another array for the factors of 10. Forty is not four + ty, and eighty is not eight + ty

Campbell Ritchie
Marshal
Posts: 68976
275

Rob Prime wrote:You'll also want to use another array for the factors of 10. Forty is not four + ty, and eighty is not eight + ty

When my daughter taught English in Indonesia she pointed out how many people said "threety" for "thirty". She thought it endearing, but still had to take a mark off for it.

Jacob Sonia
Ranch Hand
Posts: 185
Hi,

Thanks for all the inputs. But my first doubt is why are we separating into different arrays. What added advantage would it give. I see that the complexity of an array for getting things at an index is O(1) so how will it make it more efficient.

Thanks for the replies once again.

Bartender
Posts: 1561

Jacob Sonia wrote:
Thanks for all the inputs. But my first doubt is why are we separating into different arrays. What added advantage would it give. I see that the complexity of an array for getting things at an index is O(1) so how will it make it more efficient.

Efficiency is not the crux of this current problem (though this is not to say it never is the main issue), simplicity, correctness, and understandability are the issues here, and using separate arrays to keep like with like will make your program much simpler to implement, to upgrade, and to have others understand.

lowercase baba
Posts: 12834
52
Readability is probably the single most important thing to consider when reading code. if I saw this:

I would have NO CLUE what that meant. I'd have to refer back to your array each time, and count over to find out what the 23rd element is.

However, this:

makes more sense - especially after seeing a string array declared as such:

static String[] tens = { "", "teen", "twenty", "Thirty", "Forty"...}

 Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad: Two software engineers solve most of the world's problems in one K&R sized book https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton