• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program to print numbers from 0 to 100 in words

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"...}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic