Tag Archives: pythagorean triple

Java program to check whether the numbers are pythagorean triple or not

A Pythagorean triple consists of three positive integers x, y, and z, such that x2 + y2 = z2. Such a triple is commonly written (x, y, z), and a well-known example is (3, 4, 5) , i.e 32 + 42 = 9 + 16 = 25 = 52. If (x, y, z) is a Pythagorean triple, then so is (kx, ky, kz) for any positive integer k. A primitive Pythagorean triple is one in which x, y and z are coprime. A right triangle whose sides form a Pythagorean triple is called a Pythagorean triangle.

// java program to check whether the numbers are pythagorean triple or not
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PythagoreanTriple {
    public static void main(String[] args) {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            System.out.println("Enter number1:");
            String input = br.readLine();
            int a = Integer.parseInt(input);
            System.out.println("Enter number2:");
            input = br.readLine();
            int b = Integer.parseInt(input);
            System.out.println("Enter number3:");
            input = br.readLine();
            int c = Integer.parseInt(input);
            System.out.println("You Entered:" + a + " " + b + " " + c);
            if ((Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2))
                    || (Math.pow(b, 2) + Math.pow(c, 2) == Math.pow(a, 2))
                    || (Math.pow(a, 2) + Math.pow(c, 2) == Math.pow(b, 2))) {
                System.out.println("The Integers are pythagorean triple");

            } else {
                System.out.println("The Integers are not pythagorean triple");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}