# Program to find the Centroid of the triangle

Given the vertices of the triangle. The task is to find the centroid of the triangle:

**Examples:**

Input: A(1, 2), B(3, -4), C(6, -7) Output: (3.33, -3) Input: A(6, 2), B(5, -9), C(2, -7) Output: (6.5, -9)

**Approach:**Suppose if the vertices of a triangle are (x1, y1) (x2, y2) (x3, y3) then centroid of the triangle can be find from the following formula:

## C++

`// CPP program to find the centroid of triangle ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// coordinate of the vertices ` ` ` `float` `x1 = 1, x2 = 3, x3 = 6; ` ` ` `float` `y1 = 2, y2 = -4, y3 = -7; ` ` ` ` ` `// Formula to calculate centroid ` ` ` `float` `x = (x1 + x2 + x3) / 3; ` ` ` `float` `y = (y1 + y2 + y3) / 3; ` ` ` ` ` `cout << setprecision(3); ` ` ` `cout << ` `"Centroid = "` ` ` `<< ` `"("` `<< x << ` `", "` `<< y << ` `")"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the centroid of triangle ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `// coordinate of the vertices ` ` ` `float` `x1 = ` `1` `, x2 = ` `3` `, x3 = ` `6` `; ` ` ` `float` `y1 = ` `2` `, y2 = -` `4` `, y3 = -` `7` `; ` ` ` ` ` `// Formula to calculate centroid ` ` ` `float` `x = (x1 + x2 + x3) / ` `3` `; ` ` ` `float` `y = (y1 + y2 + y3) / ` `3` `; ` ` ` ` ` `//System.out.print(setprecision(3)); ` ` ` `System.out.println(` `"Centroid = "` ` ` `+ ` `"("` `+ x + ` `", "` `+ y + ` `")"` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to find ` `# the centroid of triangle ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# coordinate of the vertices ` ` ` `x1, x2, x3 ` `=` `1` `, ` `3` `, ` `6` ` ` `y1, y2, y3 ` `=` `2` `, ` `-` `4` `, ` `-` `7` ` ` ` ` `# Formula to calculate centroid ` ` ` `x ` `=` `round` `((x1 ` `+` `x2 ` `+` `x3) ` `/` `3` `, ` `2` `) ` ` ` `y ` `=` `round` `((y1 ` `+` `y2 ` `+` `y3) ` `/` `3` `, ` `2` `) ` ` ` ` ` `print` `(` `"Centroid ="` `,` `"("` `,x,` `","` `,y,` `")"` `) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the ` `// centroid of triangle ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `// coordinate of the vertices ` ` ` `float` `x1 = 1, x2 = 3, x3 = 6; ` ` ` `float` `y1 = 2, y2 = -4, y3 = -7; ` ` ` ` ` `// Formula to calculate centroid ` ` ` `float` `x = (x1 + x2 + x3) / 3; ` ` ` `float` `y = (y1 + y2 + y3) / 3; ` ` ` ` ` `//System.out.print(setprecision(3)); ` ` ` `Console.Write(` `"Centroid = "` `+ ` ` ` `"("` `+ x + ` `", "` `+ y + ` `")"` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by RaJ ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// centroid of triangle ` ` ` `// Driver code ` ` ` `// coordinate of the vertices ` `$x1` `= 1; ` `$x2` `= 3 ; ` `$x3` `= 6; ` `$y1` `= 2; ` `$y2` `= -4; ` `$y3` `= -7; ` ` ` `// Formula to calculate centroid ` `$x` `= ` `round` `((` `$x1` `+ ` `$x2` `+ ` `$x3` `) / 3, 2); ` `$y` `= ` `round` `((` `$y1` `+ ` `$y2` `+ ` `$y3` `) / 3, 2); ` ` ` `echo` `"Centroid = "` `. ` ` ` `"("` `.` `$x` `.` `", "` `.` `$y` `.` `")"` `; ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Centroid = (3.33, -3)

## Recommended Posts:

- Program to find area of a triangle
- Program to Find the Incenter of a Triangle
- Program to find Circumcenter of a Triangle
- Program to find the Type of Triangle from the given Coordinates
- Program to find third side of triangle using law of cosines
- Program to find the Radius of the incircle of the triangle
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Program to print Sum Triangle for a given array
- Program to print Fibonacci Triangle
- C Program to print Floyd's triangle
- Program to print hollow Triangle pattern
- Program for incrementing/decrementing triangle pattern
- Program to print Reverse Floyd's triangle
- Program to print binary right angle triangle
- Program to calculate area and perimeter of equilateral triangle

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.