-1

What Im trying to code is that a user inputs a number n and the program outputs all the numbers from 1 to n that are both prime numbers and palindrome numbers. I created a method for finding prime numbers and another for finding if a number is a palindrome. My code for outputting the integers that are both goes like

if ((prime(y)==true) && (pal(y)==n)) {
            System.out.println(y);
        }

Here is my method for finding prime numbers:

public static boolean prime(int n) {
    int x = 2;
    while (n%x>0) {
        x+=1;
    } if (x==n) {
        return true;
    } else {
        return false;
    }
}

Here's my method for finding palindrome numbers:

public static boolean pal(int n) {
    int rev = 0;
    int rmd = 0;
    while (n>0) {
        rmd = n%10;
        rev = rev*10 + rmd;
        n = n/10;
    } if (rev==n) {
        return true;
    } else {
        return false;
    }
}

I get the error that these two methods are not comparable because apparently one is boolean and the other integer. Anyone know how to fix this?

bob
  • 21
  • 1
  • 1
    When you compare the two methods, in the secod comparation: `if (pal(y) == n)` you are comparing boolean (pal) with an integer (n) – Mario J.G. Oct 17 '16 at 09:08

1 Answers1

3

Your statement says pal(y)==n, but pal(y) returns a boolean. I'm not sure what comparing it to n is supposed to do.

In general, avoid statements like foo == true. If it's already a boolean, just use foo, or for false !foo.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152