• Post Reply Bookmark Topic Watch Topic
  • New Topic

Breaking an integer including loops  RSS feed

 
mad andrei
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i write a code that determines and prints the digits that make up every three-digit number
that is divisible by 47. For example, for the digit 100 the program should print:

100
1
0
0
**********
457
4
5
7
**********
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please read our Beginner's FAQ to learn more the culture and protocols here. One important thing to note is that we won't give out code, only tips and suggestions on how to improve your code.

Have you learned about the % operator? That's going to be a key part of the solution.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you say that you need to do this for "every three-digit number that is divisible by 47"

Neither 457 nor 100 are evenly divisible by 47 so why are you using these as examples?
 
Marin Tapnoi
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is, for example, take your 457 as an input value, you can convert it to a float value (457.0) and divide by 10  (45.7).
Then convert 45.7 to a String and remove the ".", the result will be "457" (which you can split into tokens, convert each to an int, print it out, etc. as you'd like).
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marin Tapnoi wrote:Another way is, for example, take your 457 as an input value, you can convert it to a float value (457.0) and divide by 10  (45.7).
Then convert 45.7 to a String and remove the ".", the result will be "457" (which you can split into tokens, convert each to an int, print it out, etc. as you'd like).

Sorry, but that sounds a lot like Yak Shaving to me.
 
Marin Tapnoi
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you're right. I could have done that without the fuss of conversion to float as well

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it without converting to String at all. Just % and / and simple recursion.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A loop would be easier than recursion.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The recursive solution is ridiculously easy. Also, you are printing digits from left to right, which makes recursion a natural approach.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The recursive solution is ridiculously easy. Also, you are printing digits from left to right, which makes recursion a natural approach.

OK, I concede. Recursion however is not necessarily a beginner topic.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Recursion however is not necessarily a beginner topic.

I concede right back atcha
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marin Tapnoi wrote:. . . convert it to a float value (457.0) . . .
Avoid floats like the plague. You are risking imprecision with floating‑point arithmetic at the best of times, but floats are worse than doubles. If you really want to shave yaks, use BigDecimals. But that isn't how I would divide an int into its successive digits.

And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!