Pages

Tuesday 8 December 2015

Area of a Triangle on C++



Area of a Triangle: It is simply calculated by using the base and height of the triangle.




Formula:

                          $Area  = \frac{1}{2} \times Height \times Base $

Algorithm:

Steps:

  1. Enter the base and height
  2. Calculate the area using the above formula
  3. If height is not given find the corresponding angle between the two given sides
  4. Calculate the area by multiplying $\frac{1}{2}$ with two given sides of the triangle and $sine$ of the angle between the two sides.

#include "iostream"
#include "math.h"

using namespace std;

int main ()
{
    float base, height, area;

    cout << "Enter the base :";
    cin >> base;

    cout << "Enter the height :" ;
    cin >> height;

    // Find the area of triangle

    area = 0.5 * (base * height);
    cout << "Area of the triangle :" << area;

    return (0);
}

INPUT:       Enter the base: 12
                    Enter the height: 12
OUTPUT:   Area of the triangle: 72


If  height is not given but two side $AB$ and $BC$ is given and angle  ( angle $b$) between them  is given then the area is calculated as:

          $ Area = \frac{1}{2} \times base(BC) \times {({AB} \times Sin(b))} $

#include "iostream"
#include "math.h"

using namespace std;
#define PI 3.14159265

int main ()
{
    float b, c, area;
    double angle;

    cout << "Enter the 1st side of triangle :";
    cin >> b;

    cout << "Enter the 2nd side of triangle :" ;
    cin >> c;

     cout << "Enter the angle between two sides (degree) :" ;
    cin >> angle;


    // Find the area of triangle

    area = 0.5 * (b * c * sin(PI * angle/180));
    cout << "Area of the triangle :" << area;

    return (0);
}
INPUT:        Enter the 1st side of the triangle : 12
                     Enter the 2nd side of the triangle : 12
                     Enter the angle between two sides (degree) : 30
OUTPUT:    Area of the triangle: 36


No comments:

Post a Comment