C Program to Find Centroid of a Triangle

This C Program will help you to find the Centroid of a triangle.

The centroid is a point where all the three medians of the triangle intersect. Therefore, the centroid of the triangle can be found by finding the average of the x-coordinate’s value and the average of the y-coordinate’s value of all the vertices of the triangle.
Centroid of points, A, B and C is (x1+x2+x3)/3, (y1+y2+y3)/3.

C Program to Find Centroid of a Triangle

#include < stdio.h >
  int main() {
    int x1, x2, x3, y1, y2, y3;
    float x, y;
    printf("Enter x1\n");
    scanf("%d", & x1);
    printf("Enter y1\n");
    scanf("%d", & y1);
    printf("Enter x2\n");
    scanf("%d", & x2);
    printf("Enter y2\n");
    scanf("%d", & y2);
    printf("Enter x3\n");
    scanf("%d", & x3);
    printf("Enter y3\n");
    scanf("%d", & y3);
    x = x1 + x2 + x3;
    x = x / 3;
    y = y1 + y2 + y3;
    y = y / 3;
    printf("Centroid is (%.1f , %.1f)", x, y);
    return 0;
  }

Output

Enter x1                                                                                                                        
40                                                                                                                             
Enter y1                                                                                                                       
30                                                                                                                             
Enter x2                                                                                                                       
50                                                                                                                             
Enter y2                                                                                                                       
70                                                                                                                             
Enter x3                                                                                                                       
80                                                                                                                             
Enter y3                                                                                                                       
90                                                                                                                             
Centroid is (56.7 , 63.3) 

Leave a Comment

Your email address will not be published. Required fields are marked *