Given **N**-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.

**Examples:**

Input :N = 6

Output :6 2

The image below is of a triangle forming inside a Hexagon by joining vertices as shown above.

The triangle formed has two sides (AB and BC) common with that of a polygon. Similarly BC and

CD can make one triangle. With this, we can say that there will be a total of 6 triangles possible

having two sides common with that of a polygon. The second image of a hexagon,

a triangle is formed with no side common with that of a polygon.

There will be just 2 triangles possible, BFD and ACE.

Number of triangles formed are 6 and 2 with two side common and with no side common respectively.

Input :N = 7

Output :7 7

**Approach :**

- To make a triangle two side common with a polygon we will take any one side of a n-sided polygon, take one vertex of the chosen side and join an edge adjacent to the vertex of the other vertex.
- Traversing through each vertex and adjoining an edge adjacent to the vertex of the other vertex ,there will be
**N**number of triangles having two side common. - Now, to calculate the number of triangles with no side common subtract the total number of triangles with one side common and the total number of triangles with two side from the total number of triangles possible in a polygon.
- Triangles with no common side = Total triangles (
^{n}C_{3}) – one side common triangles ( n * ( n – 4 ) – two side common triangles ( n ). - Thus number of triangles with no common side with the polygon would be equal to n * ( n – 4 ) * ( n – 5 ) / 6.

**Note:**To calculate the number of triangles having one side common with that of a polygon click here

Below is the implementation of the above approach:

## C++

`// C++ program to implement ` `// the above problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the number of triangles ` `void` `findTriangles(` `int` `n) ` `{ ` ` ` `int` `num = n; ` ` ` ` ` `// print the number of triangles ` ` ` `// having two side common ` ` ` `cout << num << ` `" "` `; ` ` ` ` ` `// print the number of triangles ` ` ` `// having no side common ` ` ` `cout << num * (num - 4) * (num - 5) / 6; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initialize the number ` ` ` `// of sides of a polygon ` ` ` `int` `n; ` ` ` `n = 6; ` ` ` ` ` `findTriangles(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above problem ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the number of triangles ` `static` `void` `findTriangles(` `int` `n) ` `{ ` ` ` `int` `num = n; ` ` ` ` ` `// print the number of triangles ` ` ` `// having two side common ` ` ` `System.out.print( num + ` `" "` `); ` ` ` ` ` `// print the number of triangles ` ` ` `// having no side common ` ` ` `System.out.print( num * (num - ` `4` `) * (num - ` `5` `) / ` `6` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `// initialize the number ` ` ` `// of sides of a polygon ` ` ` `int` `n; ` ` ` `n = ` `6` `; ` ` ` ` ` `findTriangles(n); ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above problem ` ` ` `# Function to find the number of triangles ` `def` `findTriangles(n): ` ` ` `num ` `=` `n ` ` ` ` ` ` ` `# print the number of triangles ` ` ` `# having two side common ` ` ` `print` `(num, end ` `=` `" "` `) ` ` ` ` ` `# print the number of triangles ` ` ` `# having no side common ` ` ` `print` `(num ` `*` `(num ` `-` `4` `) ` `*` `(num ` `-` `5` `) ` `/` `/` `6` `) ` ` ` `# Driver code ` ` ` `# initialize the number ` `# of sides of a polygon ` `n ` `=` `6` `; ` ` ` `findTriangles(n) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above problem ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the number of triangles ` `static` `void` `findTriangles(` `int` `n) ` `{ ` ` ` `int` `num = n; ` ` ` ` ` `// print the number of triangles ` ` ` `// having two side common ` ` ` `Console.Write( num + ` `" "` `); ` ` ` ` ` `// print the number of triangles ` ` ` `// having no side common ` ` ` `Console.WriteLine( num * (num - 4) * (num - 5) / 6); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `// initialize the number ` ` ` `// of sides of a polygon ` ` ` `int` `n; ` ` ` `n = 6; ` ` ` ` ` `findTriangles(n); ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

6 2

** Time Complexity: ** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of cycles formed by joining vertices of n sided polygon at the center
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
- Polygon with maximum sides that can be inscribed in an N-sided regular polygon
- Check if number formed by joining two Numbers is Perfect Cube
- Count of nested polygons that can be drawn by joining vertices internally
- Find the remaining vertices of a square from two given vertices
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Number of cycles in a Polygon with lines from Centroid to Vertices
- Count number of triangles possible for the given sides range
- Count number of triangles possible with length of sides not exceeding N
- Count of acute, obtuse and right triangles with given sides
- Area of a polygon with given n ordered vertices
- Angle between 3 given vertices in a n-sided regular polygon
- Total number of triangles formed when there are H horizontal and V vertical lines
- Area of the circumcircle of any triangles with sides given
- Number of triangles formed from a set of points on three lines
- Number of triangles that can be formed with given N points

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.