Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I make the numbers come out backwards?  RSS feed

 
Apple Davisison
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The output shows:
3+2+1=6
when I wanted 1+2+3=6
how do I change that?

Please help.
Thanks,
Apple


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you get the digits, instead of displaying immediately, you should put them in a List of an array, then display them after the loop.

Note on your post: Thanks for trying to use code tags.  The code tags should surround your code.  Here's a link on using them.
 
Carey Brown
Bartender
Posts: 2995
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach would be to insert a loop prior to your while() loop, incrementing a divisor that is a factor of 10 (1 -> 10 -> 100 -> 1000 ...) until the factor is greater than number. Then in your while() loop go from this greatest factor back to one, using your current factor in your modulus operation.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the logarithm of the number to work out which power of 10 to start dividing by.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!