Java Write a program that will look for two perfect squares

Java

Write a program that will look for two perfect squares, both odd, that will add together to make another perfect square.

Ex--->3^2+4^2=5^2 , 5^2+12^2=13^2 , 6^2+8^2=10^2 , 7^2+242^=25^2

Solution

public class PerfectSquare
{
public static int checkPerfectSquare(int num1, int num2, int num3)
{
if((num3*num3) != (num2*num2 + num1*num1)) {
System.out.println(\"Not working: \" + num1 + \" \" + num2 + \" \" + num3);
return 0;
}
return 1;
}
public static void main(String[] args)
{
int num1, num2, num3;
for (int i = 1; i < 100; i++)
{
num1 = i;
if (num1 %2 == 0) {
num2 = (num1/2)*(num1/2) -1;
num3 = num2 + 2;
}
else {
num2 = (num1*num1 -1)/2;
num3 = (num1*num1 + 1)/2;
}
if(checkPerfectSquare(num1, num2, num3) == 1)
{
System.out.println(\"Perfect squares: \" + num1 + \" \" + num2 + \" \" + num3);
}
}
}
}

// Please comment if anything is not clear.

Java Write a program that will look for two perfect squares, both odd, that will add together to make another perfect square. Ex--->3^2+4^2=5^2 , 5^2+12^2=13

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site