Java: 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 Classes
A Java class is declared with the keywords public class along with a unique class name mirroring its file name. For example, in a file Hello.java in project helloworld:
package helloworld; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * @author seth * A GUI hello world. */ public class Hello { // this is an empty class }
You can declare variables and functions inside a class. In Java, variables within a class are called fields.
Ref: https://red.ht/3sL3sSl
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- 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
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook