Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Pythagorean Triplet Checker


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]

📐 Concept

Three numbers form a Pythagorean Triplet if:

               h2=p2+b2

Where:

  • h = largest number (hypotenuse)
  • p, b = the other two numbers

Example:
3, 4, 5 → 5² = 3² + 4²

import java.util.Scanner;

 

public class PythagoreanTriplet {

    public static void main(String[] args) {

 

        // Title

        System.out.println("PYTHAGOREAN TRIPLET CHECKER");

        System.out.println("---------------------------");

 

        Scanner sc = new Scanner(System.in);

 

        int a, b, c;

        int h, p, base;

 

        System.out.print("Enter first number: ");

        a = sc.nextInt();

 

        System.out.print("Enter second number: ");

        b = sc.nextInt();

 

        System.out.print("Enter third number: ");

        c = sc.nextInt();

 

        // Finding the largest number (hypotenuse)

        if (a >= b && a >= c) {

            h = a;

            p = b;

            base = c;

        } else if (b >= a && b >= c) {

            h = b;

            p = a;

            base = c;

        } else {

            h = c;

            p = a;

            base = b;

        }

 

        // Check Pythagorean condition

        if (h * h == (p * p + base * base))

            System.out.println("The given numbers form a Pythagorean Triplet.");

        else

            System.out.println("The given numbers do NOT form a Pythagorean Triplet.");

 

        sc.close();

    }

}

Output

Sample Run 1 

Enter first number: 3
Enter second number: 4
Enter third number: 5
The given numbers form a Pythagorean Triplet.

Sample Run 2 
Enter first number: 5
Enter second number: 6
Enter third number: 7
The given numbers do NOT form a Pythagorean Triplet.

📝 Explanation

  • Program accepts three integers
  • Finds the largest number and assumes it as the hypotenuse
  • Squares all values and checks:

                    h² = p² + b²

  • Displays result accordingly