C Program to Find the Sides of a Triangle

In this C programming tutorial, we will learn how to find the Sides of Triangle in C programming language. We will also have a look at how to find the area of a triangle using the C programming language.

C Program to Find the Sides of a Triangle

#include<stdio.h>
#include<math.h>

float distance(int x1,int x2,int y1,int y2)
{
    float distance;
    distance=sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
      return distance;
}
int main()
{
    int x1,x2,x3,y1,y2,y3;
    float a1,a2,a3;
    
    printf("Enter the x-coordinate of vertex A\n");
    scanf("%d",&x1);
    printf("Enter the y-coordinate of vertex A\n");
    scanf("%d",&y1);
    printf("Enter the x-coordinate of vertex B\n");
    scanf("%d",&x2);
    printf("Enter the y-coordinate of vertex B\n");
    scanf("%d",&y2);
    printf("Enter the x-coordinate of vertex C\n");
    scanf("%d",&x3);
    printf("Enter the y-coordinate of vertex C\n");
    scanf("%d",&y3);
    a1=distance(x1,x2,y1,y2);
    a2=distance(x2,x3,y2,y3);
    a3=distance(x1,x3,y1,y3);
    printf("Length of side AB is %.1f\n",a1);
    printf("Length of side BC is %.1f\n",a2);
    printf("Length of side AC is %.1f\n",a3);
    
    return 0;
}

Output

Enter the x-coordinate of vertex A                                                                                              
3                                                                                                                               
Enter the y-coordinate of vertex A                                                                                              
4                                                                                                                               
Enter the x-coordinate of vertex B                                                                                              
5                                                                                                                               
Enter the y-coordinate of vertex B                                                                                              
7                                                                                                                               
Enter the x-coordinate of vertex C                                                                                              
10                                                                                                                              
Enter the y-coordinate of vertex C                                                                                              
2                                                                                                                               
Length of side AB is 3.6                                                                                                        
Length of side BC is 7.1                                                                                                        
Length of side AC is 7.3

Area of a Triangle in C Programming Language

C program to determine the area of a triangle using Heron’s formula. In this program, the user is asked the lengths of the sides of the triangle. The triangle exists if the sum of any of its two sides is greater than the third side. The program assumes that a user will enter valid input.

C Program to Find the Area of a triangle

#include <stdio.h>
#include <math.h>

int main()
{
  double a, b, c, s, area;
  printf("Please enter the sides of a triangle\n");
  scanf("%lf%lf%lf", &a, &b, &c);
  s = (a+b+c)/2; // Semiperimeter
  area = sqrt(s*(s-a)*(s-b)*(s-c));
  printf("Area of the triangle = %.2lf\n", area);
  return 0;
}

Output

Please enter the sides of a triangle                                                                                          
7                                                                                                                             
8                                                                                                                             
12                                                                                                                            
Area of the triangle = 26.91

1 thought on “C Program to Find the Sides of a Triangle”

  1. Pingback: Midpoint Coordinate of the line in C - Code At Glance

Leave a Comment

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