# Find number of diagonals in n sided convex polygon

Given n > 3, find number of diagonals in n sided convex polygon.

According to Wikipedia, In geometry, a diagonal is a line segment joining two vertices of a polygon or polyhedron, when those vertices are not on the same edge. Informally, any sloping line is called diagonal.

**Examples :**

Input : 5 Output : 5

Explanation: Five possible diagonals are : AC, AD, BD, BE, CE

## Solution :

Since for an n-sided convex polygon, from each vertex, we can draw n-3 diagonals leaving two adjacent vertices and itself. Following this way for n-vertices, there will be n*(n-3) diagonals but then we will be calculating each diagonal twice so total number of diagonals become n*(n-3)/2

Here is code for above formula.

## C++

`#include <iostream> ` `using` `namespace` `std; ` ` ` `// C++ function to find number of diagonals ` `// in n sided convex polygon ` `int` `numberOfDiagonals(` `int` `n) ` `{ ` ` ` `return` `n * (n - 3) / 2; ` `} ` ` ` `// driver code to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << n << ` `" sided convex polygon have "` `; ` ` ` `cout << numberOfDiagonals(n) << ` `" diagonals"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java function to find number of diagonals ` `// in n sided convex polygon ` ` ` `public` `class` `Diagonals { ` ` ` ` ` `static` `int` `numberOfDiagonals(` `int` `n) ` ` ` `{ ` ` ` `return` `n * (n - ` `3` `) / ` `2` `; ` ` ` `} ` ` ` ` ` `// driver code to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.print(n + ` `" sided convex polygon have "` `); ` ` ` `System.out.println(numberOfDiagonals(n) + ` `" diagonals"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Saket Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find number of diagonals ` `# in n sided convex polygon ` `def` `numberOfDiagonals(n): ` ` ` `return` `n ` `*` `(n ` `-` `3` `) ` `/` `2` ` ` ` ` `# driver code to test above function ` `def` `main(): ` ` ` `n ` `=` `5` ` ` `print` `(n , ` `" sided convex polygon have "` `) ` ` ` `print` `(numberOfDiagonals(n) , ` `" diagonals"` `) ` ` ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `main() ` ` ` `#this code contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# function to find number of diagonals ` `// in n sided convex polygon ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `numberOfDiagonals(` `int` `n) ` ` ` `{ ` ` ` `return` `n * (n - 3) / 2; ` ` ` `} ` ` ` ` ` `// driver code to test above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.Write(n + ` `" sided convex polygon have "` `); ` ` ` ` ` `Console.WriteLine(numberOfDiagonals(n) + ` ` ` `" diagonals"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP function to find number ` `// of diagonals in n sided ` `// convex polygon ` `function` `numberOfDiagonals(` `$n` `) ` `{ ` ` ` `return` `$n` `* (` `$n` `- 3) / 2; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `echo` `$n` `, ` `" sided convex polygon have "` `; ` `echo` `numberOfDiagonals(` `$n` `) , ` ` ` `" diagonals"` `; ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5 sided convex polygon have 5 diagonals

This article is contributed by **Pratik Chhajer**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- How to check if a given point lies inside or outside a polygon?
- Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
- Given n line segments, find if any two segments intersect
- Convex Hull | Set 2 (Graham Scan)
- Find if two rectangles overlap
- Minimum Cost Polygon Triangulation
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Find Simple Closed Path for a given set of points
- Number of Integral Points between Two Points
- Area of a polygon with given n ordered vertices
- Program to find the Type of Triangle from the given Coordinates
- n'th Pentagonal Number
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Find perimeter of shapes formed with 1s in binary matrix
- Paper Cut into Minimum Number of Squares