I am trying to figure out why this code is not working. I am trying to get the Pythagoreum triples, 1-500
, where the output values are distinct.
When I try to compile, I get an error message saying
error: bad operand types for binary operator.
What am I doing wrong?
public class Pythagoras {
public static void main(String[] args) {
int side1;
int side2;
int hypotenuse;
for(side1 = 1; side1 <= 500; side1 ++)
for(side2 = 1; side2 <= 500; side2 ++)
for(hypotenuse = 1; hypotenuse <= 500; hypotenuse ++)
if(side1 < side2 < hypotenuse)
if((side1 * side1) + (side2 * side2) == (hypotenuse * hypotenuse))
System.out.printf(%d %d %d\n, side1, side2, hypotenuse);
}
}
There are a couple of issues which need to be fixed:
a < b < c
is an invalid statement, and you need to use(a < b && b < c)
You can also get rid of the comparison (
side1 < side2 < hypotenuse
) by simply updating your for loops as shown below.Updated implementation: