Program to find the Radius of the incircle of the triangle

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 <bits/stdc++.h>using namespace std;// Function to find the radius// of the incirclefloat 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 codeint 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 triangleimport java.io.*;class GFG { // Function to find the radius// of the incirclestatic 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 everythingfrom 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 triangleusing System;class GFG {// Function to find the radius// of the incirclepublic 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 codepublic 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
<?php// PHP Program to find the radius// of the incircle of the given triangle// Function to find the radius// of the incirclefunction findRadiusOfIncircle($a, $b, $c){ // the sides cannot be negative if ($a < 0 || $b < 0 || $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// Get the sides of the triangle$a = 2; $b = 2; $c = 3;// Find the radius of the incircleecho findRadiusOfIncircle($a, $b, $c) . "\n";// This code is contributed // by Akanksha Rai(Abby_akku) |
Javascript
<script>// javascript Program to find the radius// of the incircle of the given triangle // Function to find the radius// of the incirclefunction findRadiusOfIncircle(a , b , c){ // the sides cannot be negative if (a < 0 || b < 0 || c < 0) return -1; // semi-perimeter of the circle var p = (a + b + c) / 2; // area of the triangle var area = Math.sqrt(p * (p - a) * (p - b) * (p - c)); // Radius of the incircle var radius = area / p; // Return the radius return radius;}// Driver code// Get the sides of the trianglevar a = 2, b = 2, c = 3;// Find the radius of the incircledocument.write( findRadiusOfIncircle(a, b, c).toFixed(6));// This code contributed by shikhasingrajput </script> |
Output:
0.566947
Time complexity: O(log(n)) because using inbuilt sqrt function
Auxiliary Space: O(1), since no extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




