- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
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
