Breaking

Tuesday, February 19, 2019

#4: C Program To find The Perimeter and Area Of Triangle

Area & Perimeter Of Triangle


C Program to find Area of a Triangle and Perimeter of a Triangle

This program for area of triangle in c allows the user to enter three sides of the triangle. Using those values we will calculate the Perimeter of a triangle, Semi Perimeter of a triangle and then Area of a Triangle.

Source Code:

/* C Program to find Area of a Triangle and Perimeter of a Triangle */

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

int main()
{
  float a, b, c, Perimeter, s, Area;
  
  printf("\nPlease Enter three sides of triangle\n");
  scanf("%f%f%f",&a,&b,&c);
   
  Perimeter = a+b+c;
  s = (a+b+c)/2;
  Area = sqrt(s*(s-a)*(s-b)*(s-c));
   
  printf("\n Perimeter of Traiangle = %.2f\n", Perimeter);
  printf("\n Semi Perimeter of Traiangle = %.2f\n",s);
  printf("\n Area of triangle = %.2f\n",Area);

  return 0;
}

Output Console:


NOTE: Please be careful while placing the open and close brackets, it may change the entire calculation if you place it wrong

Analysis:

Step 1: User will enter the three sides of the triangle a, b, c.
Step 2: Calculating the Perimeter of Triangle using the formula P = a+b+c.
Step 3: Calculating the semi perimeter using the formula (a+b+c)/2. Although we can write semi perimeter = (Perimeter/2) but we want show the formula behind. Thats why we used standard formula
Step 4: Calculating the Area of a triangle using Heron’s Formula:
sqrt(s*(s-a)*(s-b)*(s-c));
sqrt() is the math function, which is used to calculate the square root.

No comments:

Post a Comment

Popular Posts

Post Top Ad

Your Ad Spot

Pages