Program to check congruency of two triangles

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. 
- 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 congruencyint 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 congruencyint 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 congruencyint 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 congruencyint 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 congruencyint 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 congruencydef 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 congruencydef 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 congruencydef 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 congruencydef 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 congruencydef 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 congruencysss = 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 congruencyaas = cong_aas(s1, s2, a1, a2)Â
# function call for HL congruencyhl = 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 congruencyfunction 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 congruencyfunction 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 congruencyfunction 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 congruencyfunction 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 congruencyfunction 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 Codes1 = [3, 4, 5];s2 = [4, 3, 5];Â Â Â Â Â Â Â Â Â a1 = [90, 60, 30];a2 = [60, 30, 90];Â
// function call for SSS congruencysss = cong_sss(s1, s2);Â
// function call for SAS congruencysas = cong_sas(s1, s2, a1, a2);Â
// function call for ASA congruencyasa = cong_asa(s1, s2, a1, a2);Â
// function call for AAS congruencyaas = cong_aas(s1, s2, a1, a2);Â
// function call for HL congruencyhl = cong_hl(s1, s2, );Â
// Check if triangles are congruent or notif (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 zambiatek!



