In the previous article, we have seen Java Program to Find Volume and Surface Area of Cuboid
In this article we are going to see how to check Whether Given Four Points Form Pythagorean Quadruple using Java programming language.
Java Program to Check Whether Given Four Points Form Pythagorean Quadruple
Before Jumping into the program directly let’s see how we can Check Whether Given Four Points Form Pythagorean Quadruple.
Explanation:
Let l be the length, b be the breadth, h be the height and d be the diagonal of the quadruple. If (l*l)+(b*b)+(h*h)=(d*d) then it is said to be Pythagorean quadruple.
Example:
Suppose we have below values l = 1 b = 2 h = 2 d = 3 Then according to Pythagorean quadruple principle, LHS (l*l)+(b*b)+(h*h) = 1+4+4 = 9 RHS d*d = 3*3 = 9 Since LHS = RHS Therefore it is said to be Pythagorean quadruple.
Let’s see different ways to check whether given four points form Pythagorean quadruple or not.
Method-1: Java Program to Check Whether Given Four Points Form Pythagorean Quadruple By Using Static Value
Approach:
- Declare an integer variable say ‘
l
’, ‘b
’, ‘h
’, ‘d
’ and assign the value to it, which holds the value of length, breadth, height, diagonal of quadruple respectively. - Find the LHS and RHS of the formula
(l*l)+(b*b)+(h*h)=(d*d)
- If LHS is equal to RHS then four points are from Pythagorean quadruple.
- Print the result.
Program:
class Main { public static void main(String[] args) { //length, breadth, height, diagonal value declared int l = 1; int b = 2; int h = 2; int d = 3; //find sum of (l*l)+(b*b)+(h*h) int sum = (l*l)+(b*b)+(h*h); //check the sum is equal to d*d or not if(sum == d*d) System.out.println("It is a Pythagorean quadruple"); else System.out.println("It is not a Pythagorean quadruple"); } }
Output: It is a Pythagorean quadruple
Method-2: Java Program to Check Whether Given Four Points Form Pythagorean Quadruple By Using User Input Value
Approach:
- Declare an integer variable say ‘
l
’, ‘b
’, ‘h
’, ‘d
’ and take the values as user input by using Scanner class, which holds the value of length, breadth, height and diagonal of quadruple respectively. - Find the LHS and RHS of the formula
(l*l)+(b*b)+(h*h)=(d*d)
- If LHS is equal to RHS then four points are from Pythagorean quadruple.
- Print the result.
Program:
import java.util.*; class Main { public static void main(String[] args) { //Scanner class object created Scanner s = new Scanner(System.in); System.out.println("Enter the value of l:"); int l = s.nextInt(); System.out.println("Enter the value of b:"); int b = s.nextInt(); System.out.println("Enter the value of h:"); int h = s.nextInt(); System.out.println("Enter the value of d:"); int d = s.nextInt(); //find sum of (l*l)+(b*b)+(h*h) int sum = (l*l)+(b*b)+(h*h); //check the sum is equal to d*d or not if(sum == d*d) System.out.println("It is a Pythagorean quadruple"); else System.out.println("It is not a Pythagorean quadruple"); } }
Output: Case-1 Enter the value of l: 1 Enter the value of b: 2 Enter the value of h: 2 Enter the value of d: 3 It is a Pythagorean quadruple Case-2 Enter the value of l: 3 Enter the value of b: 4 Enter the value of h: 5 Enter the value of d: 6 It is not a Pythagorean quadruple
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Articles: