SOLVED THANKS!
I wrote this program to find Pythagorean triples as long as the square of each side does not surpass 500. I seem to be getting correct results, however, I'd like to prevent the program from replicating the results in a different order, such as outputting 3, 4, 5 and then 4, 3, 5. Is there a way to do this without getting much more advanced (this is from a basic chapter on Control Statements, 2 chapters away from even using Arrays). The homework problem doesn't ask for this check, but I'd like to know for my own curiosity. Avoid straight answers, please. Hints are welcome.
Side question: the compiler does not like it if I make sideSum an integer type variable. Why is this?
Code:
public static void main(String[] args){
int side1, side2;
int hypot;
double sideSum;
System.out.printf("Side 1\tSide 2\tHypotenuse\n");
for(side1 = 1; Math.pow(side1 , 2) <= 500; side1++){
for(side2 = 1; Math.pow(side2 , 2) <= 500; side2++){
for(hypot = 1; Math.pow(hypot , 2) <= 500; hypot++){
sideSum = Math.pow(side1 , 2) + Math.pow(side2 , 2);
if(sideSum == Math.pow(hypot , 2)){
System.out.println(side1 + "\t" + side2 + "\t" + hypot);
}
}
}
}
}
Side question: the compiler does not like it if I make sideSum an integer type variable. Why is this?
I can avoid this error by simply making the variable of double type, but I'd like to know why. I didn't think squaring something would ever have the need for a floating-point answer?possible loss of precision
found : double
required: int
sideSum = Math.pow(side1 , 2) + Math.pow(side2 , 2);
^
1 error
Tool completed with exit code 1
Last edited: