Related Articles

# Program to find the Radius of the incircle of the triangle

• Last Updated : 11 Jun, 2021

Given a circle which is the incircle of a triangle whose sides are a, b< and c, the task is to find the radius of this incircle.

Examples:

```Input: a = 2, b = 2, c = 3
Output: 0.566947

Input: a = 3, b = 4, c = 5
Output: 1```

Approach:

Radius of the incircle = area of the triangle / half of perimeter of the triangle
where:
Area of the triangle = √(p*(p-a)*(p-b)*(p-c)
perimeter of the triangle = (a + b + c)

Below is the implementation of the above approach:

## C++

 `// C++ Program to find the radius``// of the incircle of the given triangle` `#include ``using` `namespace` `std;` `// Function to find the radius``// of the incircle``float` `findRadiusOfIncircle(``float` `a, ``float` `b, ``float` `c)``{` `    ``// the sides cannot be negative``    ``if` `(a < 0 || b < 0 || c < 0)``        ``return` `-1;` `    ``// semi-perimeter of the circle``    ``float` `p = (a + b + c) / 2;` `    ``// area of the triangle``    ``float` `area = ``sqrt``(p * (p - a) * (p - b) * (p - c));` `    ``// Radius of the incircle``    ``float` `radius = area / p;` `    ``// Return the radius``    ``return` `radius;``}` `// Driver code``int` `main()``{` `    ``// Get the sides of the triangle``    ``float` `a = 2, b = 2, c = 3;` `    ``// Find the radius of the incircle``    ``cout << findRadiusOfIncircle(a, b, c)``         ``<< endl;` `    ``return` `0;``}`

## Java

 `// Java Program to find the radius``// of the incircle of the given triangle``import` `java.io.*;` `class` `GFG {``  ` `// Function to find the radius``// of the incircle``static` `float` `findRadiusOfIncircle(``float` `a, ``float` `b, ``float` `c)``{` `    ``// the sides cannot be negative``    ``if` `(a < ``0` `|| b < ``0` `|| c < ``0``)``        ``return` `-``1``;` `    ``// semi-perimeter of the circle``    ``float` `p = (a + b + c) / ``2``;` `    ``// area of the triangle``    ``float` `area = (``float``)Math.sqrt(p * (p - a) * (p - b) * (p - c));` `    ``// Radius of the incircle``    ``float` `radius = area / p;` `    ``// Return the radius``    ``return` `radius;``}` `// Driver code` `    ``public` `static` `void` `main (String[] args) {``        ` `    ``// Get the sides of the triangle``    ``float` `a = ``2``, b = ``2``, c = ``3``;` `    ``// Find the radius of the incircle``    ``System.out.println( findRadiusOfIncircle(a, b, c));``        ` `    ``}``}`` ``// This code is contributed by ajit`

## Python3

 `# Python Program to find the radius``# of the incircle of the given triangle` `# from math lib. import everything``from` `math ``import` `*` `# Function to find the radius``# of the incircle``def` `findRadiusOfIncircle(a, b, c) :` `    ``# the sides cannot be negative``    ``if` `(a < ``0` `or` `b < ``0` `or` `c < ``0``) :``        ``return` `-``1` `    ``# semi-perimeter of the circle``    ``p ``=` `(a ``+` `b ``+` `c) ``/` `2` `    ``# area of the triangle``    ``area ``=` `sqrt(p ``*` `(p ``-` `a) ``*``               ``(p ``-` `b) ``*` `(p ``-` `c))` `    ``# Radius of the incircle``    ``radius ``=` `area ``/` `p` `    ``# Return the radius``    ``return` `radius` `# Driver code    ``if` `__name__ ``=``=` `"__main__"` `:` `    ``# Get the sides of the triangle``    ``a, b, c ``=` `2``, ``2``, ``3` `    ``# Find the radius of the incircle``    ``print``(``round``(findRadiusOfIncircle(a, b, c), ``6``))` `# This code is contributed by ANKITRAI1`

## C#

 `// C# Program to find the radius``// of the incircle of the given triangle``using` `System;` `class` `GFG``{` `// Function to find the radius``// of the incircle``public` `static` `float` `findRadiusOfIncircle(``float` `a,``                                         ``float` `b,``                                         ``float` `c)``{` `    ``// the sides cannot be negative``    ``if` `(a < 0 || b < 0 || c < 0)``        ``return` `-1;``    ` `    ``// semi-perimeter of the circle``    ``float` `p = (a + b + c) / 2;``    ` `    ``// area of the triangle``    ``float` `area = (``float``)Math.Sqrt(p * (p - a) *``                          ``(p - b) * (p - c));``    ` `    ``// Radius of the incircle``    ``float` `radius = area / p;``    ` `    ``// Return the radius``    ``return` `(``float``)(radius);``}` `// Driver code``public` `static` `void` `Main()``{` `    ``// Get the sides of the triangle``    ``float` `a = 2, b = 2, c = 3;` `    ``// Find the radius of the incircle``    ``Console.WriteLine(findRadiusOfIncircle(a, b, c));``}``}` `// This code is contributed``// by Shivi_Aggarwal`

## PHP

 `

## Javascript

 ``
Output:
`0.566947`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up