Area of largest triangle that can be inscribed within a rectangle - GeeksforGeeks

16 Mar, 2021
Given a rectangle of length and breadth . The task is to find the area of the biggest triangle that can be inscribed in it.Examples:  
Input: L = 5, B = 4
Output: 10

Input: L = 3, B = 2
Output: 3
 
 
From the figure, it is clear that the largest triangle that can be inscribed in the rectangle, should stand on the same base & has height raising between the same parallel sides of the rectangle.So, the base of the triangle = B Height of the triangle = LTherefore Area,  
A = (L*B)/2
Note: It should also be clear that if base of the triangle = diagonal of rectangle, still the area of triangle so obtained = lb/2 as diagonal of a rectangle divides it into 2 triangles of equal area.Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the biggest triangle// which can be inscribed within the rectangle#include <bits/stdc++.h>using namespace std; // Function to find the area// of the trianglefloat trianglearea(float l, float b){     // a and b cannot be negative    if (l < 0 || b < 0)        return -1;     // area of the triangle    float area = (l * b) / 2;    return area;} // Driver codeint main(){    float l = 5, b = 4;    cout << trianglearea(l, b) << endl;    return 0;}
// Java Program to find the biggest triangle// which can be inscribed within the rectangleimport java.util.*; class GFG{    // Function to find the area    // of the triangle    static float trianglearea(float l, float b)    {             // a and b cannot be negative        if (l < 0 || b < 0)            return -1;             // area of the triangle        float area = (l * b) / 2;        return area;    }         // Driver code    public static void main(String args[])    {        float l = 5, b = 4;                 System.out.println(trianglearea(l, b));    }}
# Python3 Program to find the# biggest triangle which can be# inscribed within the rectangle # Function to find the area# of the triangledef trianglearea(l, b) :     # a and b cannot be negative    if (l < 0 or b < 0) :        return -1     # area of the triangle    area = (l * b) / 2    return area # Driver codel = 5b = 4print(trianglearea(l, b)) # This code is contributed# by Yatin Gupta
// C# Program to find the biggest// triangle which can be inscribed// within the rectangleusing System; class GFG{// Function to find the area// of the trianglestatic float trianglearea(float l,                          float b){     // a and b cannot be negative    if (l < 0 || b < 0)        return -1;     // area of the triangle    float area = (l * b) / 2;    return area;} // Driver codepublic static void Main(){    float l = 5, b = 4;         Console.WriteLine(trianglearea(l, b));}} // This code is contributed// by inder_verma
<?php// PHP Program to find the biggest// triangle which can be inscribed// within the rectangle // Function to find the area// of the trianglefunction trianglearea($l, $b){     // a and b cannot be negative    if ($l < 0 or $b < 0)        return -1;     // area of the triangle    $area = ($l * $b) / 2;    return $area;} // Driver code$l = 5; $b = 4;echo trianglearea($l, $b); // This code is contributed// by inder_verma?>
<script> // javascript Program to find the biggest triangle// which can be inscribed within the rectangle // Function to find the area// of the trianglefunction trianglearea( l,  b){     // a and b cannot be negative    if (l < 0 || b < 0)        return -1;     // area of the triangle    let area = (l * b) / 2;    return area;} // Driver code     let l = 5, b = 4;    document.write( trianglearea(l, b) ); // This code contributed by aashish1995 </script>
10
 
inderDuMCA
YatinGupta
aashish1995
Geometric
Mathematical
Technical Scripter
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull using Divide and Conquer Algorithm
Orientation of 3 ordered points
Equation of circle when three points on the circle are given
Program to find slope of a line
Program to find line passing through 2 Points
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7