programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Breaking an integer including loops

Greenhorn
Posts: 1
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
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
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
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
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
Actually, you're right. I could have done that without the fuss of conversion to float as well

Junilu Lacar
Sheriff
Posts: 11494
180
You can do it without converting to String at all. Just % and / and simple recursion.

Carey Brown
Saloon Keeper
Posts: 3329
46
A loop would be easier than recursion.

Junilu Lacar
Sheriff
Posts: 11494
180
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
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
Carey Brown wrote:Recursion however is not necessarily a beginner topic.

I concede right back atcha

Campbell Ritchie
Marshal
Posts: 56599
172
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