# Program to find total number of edges in a Complete Graph

Given N number of vertices of a Graph. The task is to find the total number of edges possible in a *complete graph* of N vertices.

**Complete Graph:** A Complete Graph is a graph in which every pair of vertices is connected by an edge.

**Examples**:

Input : N = 3 Output : Edges = 3 Input : N = 5 Output : Edges = 10

The total number of possible edges in a complete graph of N vertices can be given as,

Total number of edges in a complete graph of N vertices= ( n * ( n – 1 ) ) / 2

**Example 1:** Below is a complete graph with N = 5 vertices.

The total number of edges in the above complete graph = 10 = (5)*(5-1)/2.

Below is the implementation of the above idea:

## C++

`// C++ implementation to find the ` `// number of edges in a complete graph ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the total number of ` `// edges in a complete graph with N vertices ` `int` `totEdge(` `int` `n) ` `{ ` ` ` `int` `result = 0; ` ` ` ` ` `result = (n * (n - 1)) / 2; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `cout << totEdge(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// number of edges in a complete graph ` ` ` `class` `GFG { ` ` ` `// Function to find the total number of ` `// edges in a complete graph with N vertices ` `static` `int` `totEdge(` `int` `n) ` `{ ` ` ` `int` `result = ` `0` `; ` ` ` ` ` `result = (n * (n - ` `1` `)) / ` `2` `; ` ` ` ` ` `return` `result; ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` `System.out.println(totEdge(n)); ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation to ` `# find the number of edges ` `# in a complete graph ` ` ` `# Function to find the total ` `# number of edges in a complete ` `# graph with N vertices ` `def` `totEdge(n) : ` ` ` ` ` `result ` `=` `(n ` `*` `(n ` `-` `1` `)) ` `/` `/` `2` ` ` ` ` `return` `result ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `6` ` ` ` ` `print` `(totEdge(n)) ` ` ` `# This code is contributed ` `# by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find ` `// the number of edges in a ` `// complete graph ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the total ` `// number of edges in a complete ` `// graph with N vertices ` `static` `int` `totEdge(` `int` `n) ` `{ ` ` ` `int` `result = 0; ` ` ` ` ` `result = (n * (n - 1)) / 2; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 6; ` ` ` `Console.Write(totEdge(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ChitraNayal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find ` `// the number of edges in a ` `// complete graph ` ` ` `// Function to find the total ` `// number of edges in a complete ` `// graph with N vertices ` `function` `totEdge(` `$n` `) ` `{ ` ` ` `$result` `= 0; ` ` ` ` ` `$result` `= (` `$n` `* (` `$n` `- 1)) / 2; ` ` ` ` ` `return` `$result` `; ` `} ` ` ` `// Driver Code ` `$n` `= 6; ` `echo` `totEdge(` `$n` `); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

15

## Recommended Posts:

- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Program to find the diameter, cycles and edges of a Wheel Graph
- Minimum number of edges between two vertices of a Graph
- Count number of edges in an undirected graph
- Number of Simple Graph with N Vertices and M Edges
- Maximum number of edges in Bipartite graph
- Minimum number of edges between two vertices of a graph using DFS
- Maximum number of edges among all connected components of an undirected graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Total number of Spanning Trees in a Graph
- Total number of Spanning trees in a Cycle Graph
- Program to find the number of region in Planar Graph
- Total number of days taken to complete the task if after certain days one person leaves
- All vertex pairs connected with exactly k edges in a graph
- Tree, Back, Edge and Cross Edges in DFS of Graph

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.