Check if given four integers (or sides) make rectangle
Given four positive integers, determine if there’s a rectangle such that the lengths of its sides are a, b, c and d (in any order).
Examples :
Input : 1 1 2 2 Output : Yes Input : 1 2 3 4 Output : No
Approach 1 :- We will check, if any of the two integers are equal and make sure rest of two are also equal using few if else conditions.
C++
// A simple program to find if given 4 // values can represent 4 sides of rectangle #include <iostream> using namespace std; // Function to check if the given // integers value make a rectangle bool isRectangle(int a, int b, int c, int d) { // Square is also a rectangle if (a == b == c == d) return true; else if (a == b && c == d) return true; else if (a == d && c == b) return true; else if (a == c && d == b) return true; else return false; } // Driver code int main() { int a, b, c, d; a = 1, b = 2, c = 3, d = 4; if (isRectangle(a, b, c, d)) cout << "Yes"; else cout << "No"; return 0; } |
chevron_right
filter_none
Java
// A simple program to find if // given 4 values can represent // 4 sides of rectangle class GFG { // Function to check if the given // integers value make a rectangle static boolean isRectangle(int a, int b, int c, int d) { // Square is also a rectangle if (a == b && a == c && a == d && c == d && b == c && b == d) return true; else if (a == b && c == d) return true; else if (a == d && c == b) return true; else if (a == c && d == b) return true; else return false; } // Driver code public static void main(String[] args) { int a = 1, b = 2, c = 3, d = 4; if (isRectangle(a, b, c, d)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by prerna saini. |
chevron_right
filter_none
Python3
# A simple program to find if given 4 # values can represent 4 sides of rectangle # Function to check if the given # integers value make a rectangle def isRectangle(a, b, c, d): # Square is also a rectangle if a == b == c == d: return True elif a == b and c == d: return True elif a == d and c == b: return True elif a == c and d == b: return True return False # Driver code a, b, c, d = 1, 2, 3, 4print("Yes" if isRectangle(a, b, c, d) else "No") # This code is contributed by Ansu Kumari. |
chevron_right
filter_none
C#
// A simple program to find if // given 4 values can represent // 4 sides of rectangle using System; class GFG { // Function to check if the given // integers value make a rectangle static bool isRectangle(int a, int b, int c, int d) { // Square is also a rectangle if (a == b && a == c && a == d && c == d && b == c && b == d) return true; else if (a == b && c == d) return true; else if (a == d && c == b) return true; else if (a == c && d == b) return true; else return false; } // Driver code public static void Main() { int a = 1, b = 2, c = 3, d = 4; if (isRectangle(a, b, c, d)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m. |
chevron_right
filter_none
Output :
No
Approach 2 :- XORing the given lengths, will given value 0.
C++
// An efficient program to find if given 4 // values can represent 4 sides of rectangle #include <iostream> using namespace std; // Function to check if the given // integers value make a rectangle bool isRectangle(int a, int b, int c, int d) { if (a ^ b ^ c ^ d) return false; else return true; } // Driver code int main() { int a, b, c, d; a = 3, b = 2, c = 3, d = 2; if (isRectangle(a, b, c, d)) cout << "Yes"; else cout << "No"; return 0; } |
chevron_right
filter_none
Java
// An efficient Java program to find if given 4 // values can represent 4 sides of rectangle class GFG { // Function to check if the given // integers value make a rectangle static boolean isRectangle(int a, int b, int c, int d) { if ((a ^ b ^ c ^ d) != 0) return false; else return true; } // Driver code public static void main(String[] args) { int a, b, c, d; a = 3; b = 2; c = 3; d = 2; if (isRectangle(a, b, c, d)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by // Smitha Dinesh Semwal |
chevron_right
filter_none
Python3
# An efficient program to find if given 4 # values can represent 4 sides of rectangle # Function to check if the given # integers value make a rectangle def isRectangle(a, b, c, d): if a ^ b ^ c ^ d: return False return True # Driver code a, b, c, d = 3, 2, 3, 2print("Yes" if isRectangle(a, b, c, d) else "No") # This code is contributed by Ansu Kumari. |
chevron_right
filter_none
C#
// An efficient C# program to find // if given 4 values can represent // 4 sides of rectangle using System; class GFG { // Function to check if the given // integers value make a rectangle static bool isRectangle(int a, int b, int c, int d) { if ((a ^ b ^ c ^ d) != 0) return false; else return true; } // Driver code public static void Main() { int a, b, c, d; a = 3; b = 2; c = 3; d = 2; if (isRectangle(a, b, c, d)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m |
chevron_right
filter_none
PHP
<?php // An efficient program to find // if given 4 values can represent // 4 sides of rectangle // Function to check if the given // integers value make a rectangle function isRectangle($a, $b, $c, $d) { if ($a ^ $b ^ $c ^ $d) return false; else return true; } // Driver code $a = 3; $b = 2; $c = 3; $d = 2; if (isRectangle($a, $b, $c, $d)) echo "Yes"; else echo "No"; // This code is contributed // by Smitha ?> |
chevron_right
filter_none
Output :
Yes
Recommended Posts:
- Check whether triangle is valid or not if sides are given
- Check if it is possible to create a polygon with given n sides
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Check if four segments form a rectangle
- Check whether a given point lies on or inside the rectangle | Set 3
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies inside a rectangle or not
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Check whether four points make a parallelogram
- Find all possible triangles with XOR of sides zero
- Maximize volume of cuboid with given sum of sides
- Area of the circumcircle of any triangles with sides given
- Count of acute, obtuse and right triangles with given sides
- Find area of triangle if two vectors of two adjacent sides are given
- Find area of parallelogram if vectors of two adjacent sides are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

