# Number of triangles formed by joining vertices of n-sided polygon with one side common

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

**Examples:**

Input :6

Output :12

The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The two triangles formed has one side (AB) common with that of a polygon.It depicts that with one edge of a hexagon we can make two triangles with one side common. We can’t take C or F as our third vertex as it will make 2 sides common with the hexagon.

No of triangles formed ie equal to 12 as there are 6 edges in a hexagon.

Input :5

Output :5

**Approach :**

- To make a triangle with one side common with a polygon the two vertices adjacent to the chosen common vertices cannot be considered as the third vertex of a triangle.
- First select any one edge from the polygon. Consider this edge to be the common edge. Number of ways to select an edge in a polygon would be equal to n.
- Now ,to form a triangle ,select any of the (n-4) vertices left .Two vertices of the common edge and two vertices adjacent to the common edge cannot be considered.
- Number of triangle formed by joining the vertices of an n-sided polygon with one side common would be equal to n * ( n – 4) .

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; ` ` ` `num = n * (n - 4); ` ` ` ` ` `// print the number of triangles ` ` ` `cout << num; ` `} ` ` ` `// 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 ` `class` `GFG ` `{ ` ` ` `// Function to find the number of triangles ` `static` `void` `findTriangles(` `int` `n) ` `{ ` ` ` `int` `num; ` ` ` `num = n * (n - ` `4` `); ` ` ` ` ` `// print the number of triangles ` ` ` `System.out.println(num); ` `} ` ` ` `// 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 ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above problem ` ` ` `# Function to find the number of triangles ` `def` `findTriangles(n): ` ` ` `num ` `=` `n ` `*` `(n ` `-` `4` `) ` ` ` ` ` `# prthe number of triangles ` ` ` `print` `(num) ` ` ` `# Driver code ` `# initialize the number of sides of a polygon ` `n ` `=` `6` ` ` `findTriangles(n) ` ` ` `# This codee is contributed by mohit kumar 29 ` |

*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; ` ` ` `num = n * (n - 4); ` ` ` ` ` `// print the number of triangles ` ` ` `Console.WriteLine(num); ` `} ` ` ` `// 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 ihritik ` |

*chevron_right*

*filter_none*

**Output:**

12

** Time Complexity: ** O(1)

## Recommended Posts:

- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Number of triangles that can be formed with given N points
- Number of triangles formed from a set of points on three lines
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Total number of triangles formed when there are H horizontal and V vertical lines
- Area of a polygon with given n ordered vertices
- Area of a n-sided regular polygon with given side length
- Side of a regular n-sided polygon circumscribed in a circle
- Count of distinct graphs that can be formed with N vertices
- Count the number of possible triangles
- Number of triangles after N moves
- Number of trees whose sum of degrees of all the vertices is L
- Count number of right triangles possible with a given perimeter
- Find number of diagonals in n sided convex polygon

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.