C++ program to find the Radius of the incircle of the triangle


In this tutorial, we will be discussing a program to find the radius of the incircle of a given triangle.

For this, we will be provided with the sides of a particular triangle and our task is to find the radius of the incircle in that triangle.

The formula for finding the radius of incircle is

area of the triangle/half perimeter of the triangle

Example

#include <bits/stdc++.h>
using namespace std;
//calculating the radius of incircle
float calc_radius(float a, float b, float c) {
   if (a < 0 || b < 0 || c < 0)
      return -1;
   //half perimeter of triangle
   float p = (a + b + c) / 2;      
   //area of triangle
   float area = sqrt(p * (p - a) * (p - b) * (p - c));
   float radius = area / p;
   // Return the radius
   return radius;
}
int main() {
   float a = 4, b = 7, c = 9;
   cout << calc_radius(a, b, c) << endl;
   return 0;
}

OUTPUT

1.34164

Updated on: 21-Nov-2019

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements