Given four arrays of 3 numbers each which represents sides and angles of two triangles. The task is to check if the two triangles are Congruent or not. Also print the theorem by which they are congruent. Note: All sides and angles given as input are for valid triangles. Examples:
Input : side1 = [3, 4, 5] angle1 = [90, 60, 30] side2 = [4, 3, 5] angle2 = [60, 30, 90] Output: Triangles are congruent by SSS SAS ASA AAS HL. Input : side1 = [3, 5, 6] angle1 = [80, 50, 50] side2 = [1, 1, 1] angle2 = [60, 60, 60] Output: Triangles are not congruent
Congruent triangles are two or more triangles that have all corresponding sides that are equal or a pair of sides and between angle are equal or a pair of angle and side between are equal or a pair of angle and other side are equal or hypotenuse and one side are equal. The congruency of triangles can be proved by the following theorems:
- Side-Side-Side (SSS) Congruency criteria: If all the sides of a triangle are equal to the sides of another triangle then the triangles are said to be congruent by the property of Side-Side-Side (SSS). In above triangle ABC and A’B’C’ if, AB=A’B’ and BC=B’C’ and CA=C’A’ then, triangles are congruent.
- Side-Angle-Side (SAS) Congruent criteria: If two sides of the two triangles are equal and the angle between them is same in both triangle then the triangles are said to be congruent by the property of Side-Angle-Side (SAS). In above triangle ABC and A’B’C’ if, AB=A’B’ and BC=B’C’ and = triangles are congruent.
- Angle-Side-Angle (ASA) Congruent criteria :If two angles of the two triangles are equal and the length of side between them is same in both triangle then the triangles are said to be congruent by the property of Angle-Side-Angle (ASA).In above triangle ABC and A’B’C’ if, = and = and BC=B’C’ then, triangles are congruent.
- Angle-Angle-Side (AAS) Congruent criteria :If two angles of the two triangles are equal and the length of other side is same in both triangle then the triangles are said to be congruent by the property of Angle-Angle-Side (AAS). In above triangle ABC and A’B’C’ if, = and = and CA=C’A’ then, triangles are congruent.
- Hypotenuse-Leg (HL) Congruent criteria : If the hypotenuse of the two triangles are equal and the length of any other one side is same in both triangle then the triangles are said to be congruent by the property of Hypotenuse-Leg (HL).
Below is the implementation of the above theorems.
C++
#include<bits/stdc++.h> using namespace std; // C++ program to check // similarity between two triangles. // Function for SAS congruency int cong_sas(vector< int > s1,vector< int > s2,vector< int > a1,vector< int > a2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); // Check for SAS // angle b / w two smallest sides is largest. if (s1[0] == s2[0] && s1[1] == s2[1]){ // # since we take angle b / w the sides. if (a1[2] == a2[2]){ return 1; } } if ( s1[1] == s2[1] && s1[2] == s2[2]){ if ( a1[0] == a2[0]){ return 1; } } if ( s1[2] == s2[2] && s1[0] == s2[0]){ if ( a1[1] == a2[1]){ return 1; } } return 0; } // Function for ASA congruency int cong_asa(vector< int > s1,vector< int > s2,vector< int > a1,vector< int > a2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); // Check for ASA // side b / w two smallest angle is largest. if (a1[0] == a2[0] && a1[1] == a2[1]){ // since we take side b / w the angle. if (s1[2] == s2[2]){ return 1; } } if ( a1[1] == a2[1] && a1[2] == a2[2]){ if (s1[0] == s2[0]){ return 1; } } if ( a1[2] == a2[2] && a1[0] == a2[0]){ if (s1[1] == s2[1]){ return 1; } } return 0; } // Function for AAS congruency int cong_aas(vector< int > s1, vector< int > s2, vector< int > a1, vector< int > a2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); // Check for AAS // side other two smallest angle is smallest or 2nd smallest. if (a1[0] == a2[0] && a1[1] == a2[1]){ // # since we take side other than angles. if (s1[0] == s2[0] || s1[1] == s2[1]){ return 1; } } if (a1[1] == a2[1] && a1[2] == a2[2]){ if (s1[1] == s2[1] || s1[2] == s2[2]){ return 1; } } if (a1[2] == a2[2] && a1[0] == a2[0]){ if ( s1[0] == s2[0] || s1[2] == s2[2]){ return 1; } } return 0; } // Function for HL congruency int cong_hl(vector< int > s1, vector< int > s2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); // Check for HL if (s1[2] == s2[2]){ if ( s1[1] == s2[1] || s1[0] == s2[0]){ return 1; } } return 0; } // Function for SSS congruency int cong_sss(vector< int > s1,vector< int > s2){ sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); // # Check for SSS if (s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){ return 1; } return 0; } int main() { // Driver Code vector< int > s1 = {3, 4, 5}; vector< int > s2 = {4, 3, 5}; vector< int > a1 = {90, 60, 30}; vector< int > a2 = {60, 30, 90}; // function call for SSS congruency int sss = cong_sss(s1, s2); // function call for SAS congruency int sas = cong_sas(s1, s2, a1, a2); // function call for ASA congruency int asa = cong_asa(s1, s2, a1, a2); // function call for AAS congruency int aas = cong_aas(s1, s2, a1, a2); // function call for HL congruency int hl = cong_hl(s1, s2); // Check if triangles are congruent or not if (sss || sas || asa || aas || hl){ cout << "Triangles are congruent by " << endl; if (sss) cout << "SSS " << endl; if (sas) cout << "SAS " << endl; if (asa) cout << "ASA " << endl; if (aas) cout << "AAS " << endl; if (hl) cout << "HL " << endl; } else cout << "Triangles are not congruent" << endl; } // The code is contributed by Nidhi goel |
Java
/*package whatever //do not write package name here */ // java program to check // similarity between two triangles. import java.io.*; import java.util.*; public class GFG { // Function for SAS congruency static int cong_sas( int [] s1, int [] s2, int [] a1, int [] a2){ Arrays.sort(s1); Arrays.sort(s2); Arrays.sort(a1); Arrays.sort(a2); // Check for SAS // angle b / w two smallest sides is largest. if (s1[ 0 ] == s2[ 0 ] && s1[ 1 ] == s2[ 1 ]){ // # since we take angle b / w the sides. if (a1[ 2 ] == a2[ 2 ]){ return 1 ; } } if ( s1[ 1 ] == s2[ 1 ] && s1[ 2 ] == s2[ 2 ]){ if ( a1[ 0 ] == a2[ 0 ]){ return 1 ; } } if ( s1[ 2 ] == s2[ 2 ] && s1[ 0 ] == s2[ 0 ]){ if ( a1[ 1 ] == a2[ 1 ]){ return 1 ; } } return 0 ; } // Function for ASA congruency static int cong_asa( int [] s1, int [] s2, int [] a1, int [] a2){ Arrays.sort(s1); Arrays.sort(s2); Arrays.sort(a1); Arrays.sort(a2); // Check for ASA // side b / w two smallest angle is largest. if (a1[ 0 ] == a2[ 0 ] && a1[ 1 ] == a2[ 1 ]){ // since we take side b / w the angle. if (s1[ 2 ] == s2[ 2 ]){ return 1 ; } } if ( a1[ 1 ] == a2[ 1 ] && a1[ 2 ] == a2[ 2 ]){ if (s1[ 0 ] == s2[ 0 ]){ return 1 ; } } if ( a1[ 2 ] == a2[ 2 ] && a1[ 0 ] == a2[ 0 ]){ if (s1[ 1 ] == s2[ 1 ]){ return 1 ; } } return 0 ; } // Function for AAS congruency static int cong_aas( int [] s1, int [] s2, int [] a1, int [] a2){ Arrays.sort(s1); Arrays.sort(s2); Arrays.sort(a1); Arrays.sort(a2); // Check for AAS // side other two smallest angle is smallest or 2nd smallest. if (a1[ 0 ] == a2[ 0 ] && a1[ 1 ] == a2[ 1 ]){ // # since we take side other than angles. if (s1[ 0 ] == s2[ 0 ] || s1[ 1 ] == s2[ 1 ]){ return 1 ; } } if (a1[ 1 ] == a2[ 1 ] && a1[ 2 ] == a2[ 2 ]){ if (s1[ 1 ] == s2[ 1 ] || s1[ 2 ] == s2[ 2 ]){ return 1 ; } } if (a1[ 2 ] == a2[ 2 ] && a1[ 0 ] == a2[ 0 ]){ if ( s1[ 0 ] == s2[ 0 ] || s1[ 2 ] == s2[ 2 ]){ return 1 ; } } return 0 ; } // Function for HL congruency static int cong_hl( int [] s1, int [] s2){ Arrays.sort(s1); Arrays.sort(s2); // Check for HL if (s1[ 2 ] == s2[ 2 ]){ if ( s1[ 1 ] == s2[ 1 ] || s1[ 0 ] == s2[ 0 ]){ return 1 ; } } return 0 ; } // Function for SSS congruency static int cong_sss( int [] s1, int [] s2){ Arrays.sort(s1); Arrays.sort(s2); // # Check for SSS if (s1[ 0 ] == s2[ 0 ] && s1[ 1 ] == s2[ 1 ] && s1[ 2 ] == s2[ 2 ]){ return 1 ; } return 0 ; } public static void main(String[] args) { // Driver Code int [] s1 = { 3 , 4 , 5 }; int [] s2 = { 4 , 3 , 5 }; int [] a1 = { 90 , 60 , 30 }; int [] a2 = { 60 , 30 , 90 }; // function call for SSS congruency int sss = cong_sss(s1, s2); // function call for SAS congruency int sas = cong_sas(s1, s2, a1, a2); // function call for ASA congruency int asa = cong_asa(s1, s2, a1, a2); // function call for AAS congruency int aas = cong_aas(s1, s2, a1, a2); // function call for HL congruency int hl = cong_hl(s1, s2); // Check if triangles are congruent or not if (sss == 1 || sas == 1 || asa == 1 || aas == 1 || hl == 1 ){ System.out.println( "Triangles are congruent by " ); if (sss == 1 ) System.out.println( "SSS " ); if (sas == 1 ) System.out.println( "SAS " ); if (asa == 1 ) System.out.println( "ASA " ); if (aas == 1 ) System.out.println( "AAS " ); if (hl == 1 ) System.out.println( "HL " ); } else System.out.println( "Triangles are not congruent" ); } } // The code is contributed by Nidhi goel. |
Python
# Python program to check # similarity between two triangles. # Function for SAS congruency def cong_sas(s1, s2, a1, a2): s1 = [ float (i) for i in s1] s2 = [ float (i) for i in s2] a1 = [ float (i) for i in a1] a2 = [ float (i) for i in a2] s1.sort() s2.sort() a1.sort() a2.sort() # Check for SAS # angle b / w two smallest sides is largest. if s1[ 0 ] = = s2[ 0 ] and s1[ 1 ] = = s2[ 1 ]: # since we take angle b / w the sides. if a1[ 2 ] = = a2[ 2 ]: return 1 if s1[ 1 ] = = s2[ 1 ] and s1[ 2 ] = = s2[ 2 ]: if a1[ 0 ] = = a2[ 0 ]: return 1 if s1[ 2 ] = = s2[ 2 ] and s1[ 0 ] = = s2[ 0 ]: if a1[ 1 ] = = a2[ 1 ]: return 1 return 0 # Function for ASA congruency def cong_asa(s1, s2, a1, a2): s1 = [ float (i) for i in s1] s2 = [ float (i) for i in s2] a1 = [ float (i) for i in a1] a2 = [ float (i) for i in a2] s1.sort() s2.sort() a1.sort() a2.sort() # Check for ASA # side b / w two smallest angle is largest. if a1[ 0 ] = = a2[ 0 ] and a1[ 1 ] = = a2[ 1 ]: # since we take side b / w the angle. if s1[ 2 ] = = s2[ 2 ]: return 1 if a1[ 1 ] = = a2[ 1 ] and a1[ 2 ] = = a2[ 2 ]: if s1[ 0 ] = = s2[ 0 ]: return 1 if a1[ 2 ] = = a2[ 2 ] and a1[ 0 ] = = a2[ 0 ]: if s1[ 1 ] = = s2[ 1 ]: return 1 return 0 # Function for AAS congruency def cong_aas(s1, s2, a1, a2): s1 = [ float (i) for i in s1] s2 = [ float (i) for i in s2] a1 = [ float (i) for i in a1] a2 = [ float (i) for i in a2] s1.sort() s2.sort() a1.sort() a2.sort() # Check for AAS # side other two smallest angle is smallest or 2nd smallest. if a1[ 0 ] = = a2[ 0 ] and a1[ 1 ] = = a2[ 1 ]: # since we take side other than angles. if s1[ 0 ] = = s2[ 0 ] or s1[ 1 ] = = s2[ 1 ]: return 1 if a1[ 1 ] = = a2[ 1 ] and a1[ 2 ] = = a2[ 2 ]: if s1[ 1 ] = = s2[ 1 ] or s1[ 2 ] = = s2[ 2 ]: return 1 if a1[ 2 ] = = a2[ 2 ] and a1[ 0 ] = = a2[ 0 ]: if s1[ 0 ] = = s2[ 0 ] or s1[ 2 ] = = s2[ 2 ]: return 1 return 0 # Function for HL congruency def cong_hl(s1, s2): s1 = [ float (i) for i in s1] s2 = [ float (i) for i in s2] s1.sort() s2.sort() # Check for HL if s1[ 2 ] = = s2[ 2 ]: if s1[ 1 ] = = s2[ 1 ] or s1[ 0 ] = = s2[ 0 ]: return 1 return 0 # Function for SSS congruency def cong_sss(s1, s2): s1 = [ float (i) for i in s1] s2 = [ float (i) for i in s2] s1.sort() s2.sort() # Check for SSS if (s1[ 0 ] = = s2[ 0 ] and s1[ 1 ] = = s2[ 1 ] and s1[ 2 ] = = s2[ 2 ]): return 1 return 0 # Driver Code s1 = [ 3 , 4 , 5 ] s2 = [ 4 , 3 , 5 ] a1 = [ 90 , 60 , 30 ] a2 = [ 60 , 30 , 90 ] # function call for SSS congruency sss = cong_sss(s1, s2) # function call for SAS congruency sas = cong_sas(s1, s2, a1, a2) # function call for ASA congruency asa = cong_asa(s1, s2, a1, a2) # function call for AAS congruency aas = cong_aas(s1, s2, a1, a2) # function call for HL congruency hl = cong_hl(s1, s2, ) # Check if triangles are congruent or not if sss or sas or asa or aas or hl : print "Triangles are congruent by", if sss: print "SSS", if sas: print "SAS", if asa: print "ASA", if aas: print "AAS", if hl: print "HL", else : print "Triangles are not congruent" |
C#
// C# program to check // similarity between two triangles. using System; using System.Collections.Generic; class GFG { // Function for SAS congruency static int cong_sas(List< int > s1,List< int > s2,List< int > a1,List< int > a2){ s1.Sort(); s2.Sort(); a1.Sort(); a2.Sort(); // Check for SAS // angle b / w two smallest sides is largest. if (s1[0] == s2[0] && s1[1] == s2[1]){ // # since we take angle b / w the sides. if (a1[2] == a2[2]){ return 1; } } if ( s1[1] == s2[1] && s1[2] == s2[2]){ if ( a1[0] == a2[0]){ return 1; } } if ( s1[2] == s2[2] && s1[0] == s2[0]){ if ( a1[1] == a2[1]){ return 1; } } return 0; } // Function for ASA congruency static int cong_asa(List< int > s1,List< int > s2,List< int > a1,List< int > a2){ s1.Sort(); s2.Sort(); a1.Sort(); a2.Sort(); // Check for ASA // side b / w two smallest angle is largest. if (a1[0] == a2[0] && a1[1] == a2[1]) { // since we take side b / w the angle. if (s1[2] == s2[2]){ return 1; } } if ( a1[1] == a2[1] && a1[2] == a2[2]){ if (s1[0] == s2[0]){ return 1; } } if ( a1[2] == a2[2] && a1[0] == a2[0]){ if (s1[1] == s2[1]){ return 1; } } return 0; } // Function for AAS congruency static int cong_aas(List< int > s1, List< int > s2, List< int > a1, List< int > a2){ s1.Sort(); s2.Sort(); a1.Sort(); a2.Sort(); // Check for AAS // side other two smallest angle is smallest or 2nd smallest. if (a1[0] == a2[0] && a1[1] == a2[1]) { // # since we take side other than angles. if (s1[0] == s2[0] || s1[1] == s2[1]){ return 1; } } if (a1[1] == a2[1] && a1[2] == a2[2]){ if (s1[1] == s2[1] || s1[2] == s2[2]){ return 1; } } if (a1[2] == a2[2] && a1[0] == a2[0]){ if ( s1[0] == s2[0] || s1[2] == s2[2]){ return 1; } } return 0; } // Function for HL congruency static int cong_hl(List< int > s1, List< int > s2){ s1.Sort(); s2.Sort(); // Check for HL if (s1[2] == s2[2]){ if ( s1[1] == s2[1] || s1[0] == s2[0]){ return 1; } } return 0; } // Function for SSS congruency static int cong_sss(List< int > s1,List< int > s2){ s1.Sort(); s2.Sort(); // # Check for SSS if (s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){ return 1; } return 0; } static void Main( string [] args) { // Driver Code List< int > s1 = new List< int >(){3, 4, 5}; List< int > s2 = new List< int >(){4, 3, 5}; List< int > a1 = new List< int >(){90, 60, 30}; List< int > a2 = new List< int >(){60, 30, 90}; // function call for SSS congruency int sss = cong_sss(s1, s2); // function call for SAS congruency int sas = cong_sas(s1, s2, a1, a2); // function call for ASA congruency int asa = cong_asa(s1, s2, a1, a2); // function call for AAS congruency int aas = cong_aas(s1, s2, a1, a2); // function call for HL congruency int hl = cong_hl(s1, s2); // Check if triangles are congruent or not if (sss==1 || sas==1 || asa==1 || aas==1 || hl==1){ Console.WriteLine( "Triangles are congruent by " ); if (sss==1) Console.WriteLine( "SSS " ); if (sas==1) Console.WriteLine( "SAS " ); if (asa==1) Console.WriteLine( "ASA " ); if (aas==1) Console.WriteLine( "AAS " ); if (hl==1) Console.WriteLine( "HL " ); } else Console.WriteLine( "Triangles are not congruent" ); } } |
Javascript
<script> // JavaScript program to check // similarity between two triangles. // Function for SAS congruency function cong_sas(s1, s2, a1, a2){ s1.sort(); s2.sort(); a1.sort(); a2.sort(); // Check for SAS // angle b / w two smallest sides is largest. if (s1[0] == s2[0] && s1[1] == s2[1]){ // # since we take angle b / w the sides. if (a1[2] == a2[2]){ return 1; } } if ( s1[1] == s2[1] && s1[2] == s2[2]){ if ( a1[0] == a2[0]){ return 1; } } if ( s1[2] == s2[2] && s1[0] == s2[0]){ if ( a1[1] == a2[1]){ return 1; } } return 0; } // Function for ASA congruency function cong_asa(s1, s2, a1, a2){ s1.sort(); s2.sort(); a1.sort(); a2.sort(); // Check for ASA // side b / w two smallest angle is largest. if (a1[0] == a2[0] && a1[1] == a2[1]){ // since we take side b / w the angle. if (s1[2] == s2[2]){ return 1; } } if ( a1[1] == a2[1] && a1[2] == a2[2]){ if (s1[0] == s2[0]){ return 1; } } if ( a1[2] == a2[2] && a1[0] == a2[0]){ if (s1[1] == s2[1]){ return 1; } } return 0; } // Function for AAS congruency function cong_aas(s1, s2, a1, a2){ s1.sort(); s2.sort(); a1.sort(); a2.sort(); // Check for AAS // side other two smallest angle is smallest or 2nd smallest. if (a1[0] == a2[0] && a1[1] == a2[1]){ // # since we take side other than angles. if (s1[0] == s2[0] || s1[1] == s2[1]){ return 1; } } if (a1[1] == a2[1] && a1[2] == a2[2]){ if (s1[1] == s2[1] || s1[2] == s2[2]){ return 1; } } if (a1[2] == a2[2] && a1[0] == a2[0]){ if ( s1[0] == s2[0] || s1[2] == s2[2]){ return 1; } } return 0; } // Function for HL congruency function cong_hl(s1, s2){ s1.sort(); s2.sort(); // Check for HL if (s1[2] == s2[2]){ if ( s1[1] == s2[1] || s1[0] == s2[0]){ return 1; } } return 0; } // Function for SSS congruency function cong_sss(s1, s2){ s1.sort(); s2.sort(); // # Check for SSS if (s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){ return 1; } return 0; } // Driver Code s1 = [3, 4, 5]; s2 = [4, 3, 5]; a1 = [90, 60, 30]; a2 = [60, 30, 90]; // function call for SSS congruency sss = cong_sss(s1, s2); // function call for SAS congruency sas = cong_sas(s1, s2, a1, a2); // function call for ASA congruency asa = cong_asa(s1, s2, a1, a2); // function call for AAS congruency aas = cong_aas(s1, s2, a1, a2); // function call for HL congruency hl = cong_hl(s1, s2, ); // Check if triangles are congruent or not if (sss || sas || asa || aas || hl){ document.write( "Triangles are congruent by " ); if (sss) document.write( "SSS " ); if (sas) document.write( "SAS " ); if (asa) document.write( "ASA " ); if (aas) document.write( "AAS " ); if (hl) document.write( "HL " ); } else document.write( "Triangles are not congruent" ); // The code is contributed by Nidhi goel </script> |
Triangles are congruent by SSS SAS ASA AAS HL
Time Complexity: O(1) As the arrays have only 3 elements, so the total time taken can be treated as constant.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!