ICSE Class 10 Computer Applications Question 33 of 43

Conditional Constructs in Java — Question 33

Back to all questions
33
Question

Question 33

Write a program in Java to accept three numbers and check whether they are Pythagorean Triplet or not. The program must display the message accordingly. [Hint: h2=p2+b2]

Answer
import java.util.Scanner;

public class KboatPythagoreanTriplet
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter 1st number: ");
        int a = in.nextInt();
        System.out.print("Enter 2nd number: ");
        int b = in.nextInt();
        System.out.print("Enter 3rd number: ");
        int c = in.nextInt();
        
        if (a * a + b * b == c * c ||
            a * a + c * c == b * b ||
            b * b + c * c == a * a)
            System.out.println("Pythagorean Triplets");
        else
            System.out.println("Not Pythagorean Triplets");
    }
}
Output
BlueJ output of KboatPythagoreanTriplet.java