• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Breaking an integer including loops

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
**********
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do it without converting to String at all. Just % and / and simple recursion.
 
Bartender
Posts: 10963
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A loop would be easier than recursion.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 10963
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Recursion however is not necessarily a beginner topic.


I concede right back atcha
 
Marshal
Posts: 80055
409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic