0

so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,... and while the program identify them it sends a message.

package doodle;

int num2=11;
for (int i=1; i<100; i++) {
    System.out.println(i);
    int num1=i;
    if(num1==num2) {
        System.out.println("WOW"); 
    }
} 

Thanks

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
VoltroN
  • 7
  • 3
  • what exactly are you trying to do, and what exactly is(n't) working? why do you need this num1 variable? – Stultuske Nov 05 '18 at 07:50
  • 2
    Possible duplicate of [Check if integer has repeating digits. No string methods or arrays](https://stackoverflow.com/questions/26748026/check-if-integer-has-repeating-digits-no-string-methods-or-arrays) – Mark Nov 05 '18 at 07:51
  • If you mean numbers with repeating digits between 1 and 99, then that are only the numbers that are divisible by 11. – Johannes Kuhn Nov 05 '18 at 07:56
  • What do you mean by "look alike"? numbers composed of twice the same digit? – Maurice Perry Nov 05 '18 at 07:58

3 Answers3

1

I would do using a String

for (int i = 11; i < 100; i++) {
    StringBuffer orig = new StringBuffer();
    String left = orig.append(i).toString();
    if (orig.reverse().toString().equals(left)) {
        System.out.println(left);
    }
}

or if you really wanted to use an int with flaky logic

int start = 11;
for (int i = 11; i < 100; i++) {
    if (i == start) {
        System.out.println(start);
        start += 11;
    }
}

Edit

As @mark has rightly pointed out, these solution only work whilst the range is up to 100

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Note that the first method only works for `i < 100`, after `100` it will match with palindromes of numbers aswell like `121`, `131` etc.. Which is also what the question specified but it's good to know either way. – Mark Nov 05 '18 at 07:58
  • @Mark Yep, the requirements are not clear at all, but there maybe be a limit of 100 – Scary Wombat Nov 05 '18 at 08:00
  • @Mark While true, the domain here is `11 <= i < 100` (as the for loop states), so the problem doesn't exist – BackSlash Nov 05 '18 at 08:01
  • 1
    @BackSlash Just pointing it out if someone in the future comes across this answer and decides they want to use it for `i < 1000` or something – Mark Nov 05 '18 at 08:02
  • 1
    @Mark I have updated my answer with the disclaimer. – Scary Wombat Nov 05 '18 at 08:04
1
int num2=11;
for (int i=1; i<100; i++) {
    if(i%num2==0) {  //<---- look alike
        System.out.println("WOW");
         }
suvojit_007
  • 1,690
  • 2
  • 17
  • 23
0

I would do it using String conversion and codePoint comparison

for (Integer number = 0; number < 1000; number++) {
    System.out.println(number);

    String stringnumber = String.valueOf(number);

    if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
            System.out.println("WOW");
    }
}

length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.

All numbers from 0 to Integer.MAX_VALUE can be handled.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
M. Axsta
  • 1
  • 3