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:

# Finding out if all the digits are equivalent in three digit number

Akimbas Akimbasas
Ranch Hand
Posts: 32
1
So I was asked to write a program that finds out if the digits in the three-digit number are the same. I thought a bit about it, wrote some java stuff about it, and it seems I need some tip or somethin.

When I run this, it says that even the number with equivalent digits does not have the same digits.

Niraj Jha
Ranch Hand
Posts: 63
[mg]edited out solution

Maneesh Godbole
Bartender
Posts: 11445
18
Hi Niraj,

Unfortunately I edited out your (correct) solution.
To quote from the top of this forum
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

Hope you understand.

Maneesh Godbole
Bartender
Posts: 11445
18
Akimbas Akimbasas wrote:..and it seems I need some tip or somethin.

Welcome to the Ranch.

Print out the values for
x % 100, x % 10, x % 1 and I promise you will be surprised

Niraj Jha
Ranch Hand
Posts: 63
Sure @Maneesh.

Paweł Baczyński
Bartender
Posts: 2087
44
First, figure out how to get individual digits correctly.
Then, just compare them.

Akimbas Akimbasas
Ranch Hand
Posts: 32
1
I had the feeling that the x % 1 didn't want to cooperate, but it turns out that x % 100 is wrong as well . I picked the number 222 and I got 22, 2, 0.

So I think is safe to assume that the middle number will always be right. But how about the first and the last? Simple division ? I get 2.22 if I divide by 100 but if they're integer numbers, so it should be rounded up to 2. So only the last one remains, hmm. Any more tips? Modulus and simple division doesn't seem to get me the last 2.

Johan Fourie
Greenhorn
Posts: 14
You can use modulus and simple division together. Hope this helps.

Campbell Ritchie
Marshal
Posts: 56598
172
If by middle number you mean 3 in 234, then no, it isn't right. Try…and see what happens.

Campbell Ritchie
Marshal
Posts: 56598
172
You most certainly can get the individual digits with a combination of remainder and simple division.

Akimbas Akimbasas
Ranch Hand
Posts: 32
1
Sorted it out it seems, it works without any problems. Thank you for your help, guys, I will look forward to ask for more tips. :p

Welcome to the Ranch.

Glad to be here.

Joel Christophel
Ranch Hand
Posts: 250
1
Unless you have to complete this mathematically, this problem would be most easily solved by turning the integer into a String.

Edit: I see now that you've completed the assignment. Good job

 It is sorta covered in the JavaRanch Style Guide.