Java Exercises: Check whether three given lengths of three sides form a right triangle
Java Basic: Exercise-213 with Solution
Write a Java program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No".
Input:
Integers separated by a single space. 1 ≤ length of the side ≤ 1,000
Pictorial Presentation:


Sample Solution:
Java Code:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Main {
Scanner sc = new Scanner(System.in);
public void run() {
System.out.println("Input three integers(sides of a triangle)");
int[] int_num = new int[]{
sc.nextInt(),sc.nextInt(),sc.nextInt()
};
Arrays.sort(int_num);
System.out.println("If the given sides form a right triangle?");
ln((int_num[2]*int_num[2]==int_num[0]*int_num[0]+int_num[1]*int_num[1])?"Yes":"No");
}
public static void main(String[] args) {
new Main().run();
}
public static void pr(Object o) {
System.out.print(o);
}
public static void ln(Object o) {
System.out.println(o);
}
public static void ln() {
System.out.println();
}
}
Sample Output:
Input three integers(sides of a triangle) 6 9 12 If the given sides form a right triangle? No
Flowchart:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to compute the digit number of sum of two given integers.
Next: Write a Java program which solve the equation. Print the values of x, y where a, b, c, d, e and f are specified
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
Java: Anagrams
Generates all anagrams of a string.
public static List<String> anagrams(String input) { if (input.length() <= 2) { return input.length() == 2 ? Arrays.asList(input, input.substring(1) + input.substring(0, 1)) : Collections.singletonList(input); } return IntStream.range(0, input.length()) .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1))) .flatMap(entry -> anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1)) .stream() .map(s -> entry.getValue() + s)) .collect(Collectors.toList()); }
Ref: https://bit.ly/3rvAdAK
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises