Trying to figure out how can I make a program to identify equal numbers

3592 views java
-1

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

answered question

what exactly are you trying to do, and what exactly is(n't) working? why do you need this num1 variable?

If you mean numbers with repeating digits between 1 and 99, then that are only the numbers that are divisible by 11.

What do you mean by "look alike"? numbers composed of twice the same digit?

3 Answers

10

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;
    }
}

posted this
10

for (int i=1; i < 100; i++) {
    String num = ""+i;
    if(num.matches("(.)\\1+"))
         System.out.println(num);
}

posted this
4

int num2=11;
for (int i=1; i<100; i++) {
    if(i%11==0) {  //<---- look alike
        System.out.println("WOW");
         }

posted this

Have an answer?

JD

Please login first before posting an answer.