Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIArea of a largest square fit in a right angle triangle

Area of a largest square fit in a right angle triangle

Given a right angled triangle with height l, base b & hypotenuse h.We need to find the area of the largest square that can fit in the right angled triangle.

Examples: 

Input: l = 3, b = 4, h = 5
Output: 2.93878
The biggest square that can fit inside 
is of 1.71428 * 1.71428 dimension

Input: l = 5, b = 12, h = 13
Output: 12.4567

Considering the above diagram, we see,tanx = l/b
Here it is also true that, tanx = a/(b-a)
So, l/b = a/(b-a) which means that, a = (l*b)/(l+b)

Below is the required implementation:  

C++




// C++ Program to find the area of the biggest square
// which can fit inside the right angled triangle
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the area of the biggest square
float squareArea(float l, float b, float h)
{
 
    // the height or base or hypotenuse
    // cannot be negative
    if (l < 0 || b < 0 || h < 0)
        return -1;
 
    // side of the square
    float a = (l * b) / (l + b);
 
    // squaring to get the area
    return a * a;
}
 
// Driver code
int main()
{
    float l = 5, b = 12, h = 13;
    cout << squareArea(l, b, h) << endl;
 
    return 0;
}


Java




//Java Program to find the area of the biggest square
//which can fit inside the right angled triangle
public class GFG {
 
    //Function to find the area of the biggest square
    static float squareArea(float l, float b, float h)
    {
 
     // the height or base or hypotenuse
     // cannot be negative
     if (l < 0 || b < 0 || h < 0)
         return -1;
 
     // side of the square
     float a = (l * b) / (l + b);
 
     // squaring to get the area
     return a * a;
    }
 
    //Driver code
    public static void main(String[] args) {
         
         float l = 5, b = 12, h = 13;
         System.out.println(squareArea(l, b, h));
    }
}


Python3




# Python 3 Program  to find the
# area of the biggest square
# which can fit inside the right
# angled triangle
 
# Function to find the area of the biggest square
def squareArea(l, b, h) :
 
    # the height or base or hypotenuse
    # cannot be negative
    if l < 0 or b < 0 or h < 0 :
        return -1
 
    # side of the square
    a = (l * b) / (l + b)
 
    # squaring to get the area
    return a * a
 
# Driver Code
if __name__ == "__main__" :
 
    l, b, h = 5, 12, 13
 
    print(round(squareArea(l, b, h),4))
 
# This code is contributed by ANKITRAI1


C#




// C# Program to find the area of
// the biggest square which can
// fit inside the right angled triangle
using System;
class GFG
{
 
// Function to find the area
// of the biggest square
static float squareArea(float l, float b,
                        float h)
{
 
// the height or base or hypotenuse
// cannot be negative
if (l < 0 || b < 0 || h < 0)
    return -1;
 
// side of the square
float a = (l * b) / (l + b);
 
// squaring to get the area
return a * a;
}
 
// Driver code
public static void Main()
{
    float l = 5, b = 12, h = 13;
    Console.WriteLine(squareArea(l, b, h));
}
}
 
// This code is contributed
// by inder_verma..


PHP




<?php
// PHP Program to find the area
// of the biggest square which
// can fit inside the right
// angled triangle
 
// Function to find the area
// of the biggest square
function squareArea($l, $b, $h)
{
 
    // the height or base or
    // hypotenuse cannot be
    // negative
    if ($l < 0 || $b < 0 || $h < 0)
        return -1;
 
    // side of the square
    $a = ($l * $b) / ($l + $b);
 
    // squaring to get the area
    return $a * $a;
}
 
// Driver code
$l = 5;
$b = 12;
$h = 13;
echo round(squareArea($l, $b, $h), 4);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// javascript Program to find the area of the biggest square
// which can fit inside the right angled triangle
 
// Function to find the area of the biggest square
function squareArea(l , b , h)
{
 
 // the height or base or hypotenuse
 // cannot be negative
 if (l < 0 || b < 0 || h < 0)
     return -1;
 
 // side of the square
 var a = (l * b) / (l + b);
 
 // squaring to get the area
 return a * a;
}
 
// Driver code
        
 var l = 5, b = 12, h = 13;
 document.write(squareArea(l, b, h).toFixed(4));
 
// This code contributed by Princi Singh
 
</script>


Output: 

12.4567

 

Time complexity: O(1)

space complexity: O(1)

Example in c :

Approach

1. Find the length of the hypotenuse of the triangle using the Pythagorean theorem.
2. Divide the hypotenuse by sqrt(2) to get the side length of the largest square that can fit inside the triangle.
3. Calculate the area of the square using the formula area = side_length * side_length.

C




#include <stdio.h>
#include <math.h>
 
int main() {
    double a, b, c, side_length, area;
    // assume a and b are the lengths of the two legs of the right-angled triangle
    a = 3.0;
    b = 4.0;
    c = sqrt(a * a + b * b);  // hypotenuse length
    side_length = c / sqrt(2);  // side length of largest square
    area = side_length * side_length;  // area of largest square
    printf("The area of the largest square that can fit inside the right-angled triangle is %f\n", area);
    return 0;
}


C++




#include <bits/stdc++.h>
using namespace std;
 
int main() {
    double a, b, c, side_length, area;
    // assume a and b are the lengths of the two legs of the right-angled triangle
    a = 3.0;
    b = 4.0;
    c = sqrt(a * a + b * b);  // hypotenuse length
    side_length = c / sqrt(2);  // side length of largest square
    area = side_length * side_length;  // area of largest square
    cout << "The area of the largest square that can fit inside the right-angled triangle is " << area << std::endl;
    return 0;
}


Java




import java.lang.Math;
 
public class Main {
    public static void main(String[] args) {
        double a, b, c, side_length, area;
        // assume a and b are the lengths of the two legs of the right-angled triangle
        a = 3.0;
        b = 4.0;
        c = Math.sqrt(a * a + b * b); // hypotenuse length
        side_length = c / Math.sqrt(2); // side length of largest square
        area = side_length * side_length; // area of largest square
        System.out.println("The area of the largest square that can fit inside the right-angled triangle is " + area);
    }
}


Python3




import math
 
a, b = 3.0, 4.0  # assume a and b are the lengths of the two legs of the right-angled triangle
c = math.sqrt(a ** 2 + b ** 2# hypotenuse length
side_length = c / math.sqrt(2# side length of largest square
area = side_length ** 2  # area of largest square
print("The area of the largest square that can fit inside the right-angled triangle is", area)


Javascript




let a = 3.0, b = 4.0; // assume a and b are the lengths of the two legs of the right-angled triangle
let c = Math.sqrt(a ** 2 + b ** 2); // hypotenuse length
let side_length = c / Math.sqrt(2); // side length of largest square
let area = side_length ** 2; // area of largest square
console.log("The area of the largest square that can fit inside the right-angled triangle is", area);


C#




using System;
 
class MainClass {
    static void Main()
    {
        double a, b, c, side_length, area;
        // assume a and b are the lengths of the two legs of
        // the right-angled triangle
        a = 3.0;
        b = 4.0;
        c = Math.Sqrt(a * a + b * b); // hypotenuse length
        side_length
            = c
              / Math.Sqrt(
                  2); // side length of largest square
        area = side_length
               * side_length; // area of largest square
        Console.WriteLine(
            "The area of the largest square that can fit inside the right-angled triangle is "
            + area);
    }
}


Output

The area of the largest square that can fit inside the right-angled triangle is 12.500000

The time complexity of this code is O(1), as all the calculations are done in constant time, regardless of the input values.

The auxiliary space of the code is also O(1), as we are only storing a few variables, which do not depend on the size of the input.

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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments