Área del cuadrado más grande que cabe en un triángulo rectángulo

Dado un triángulo rectángulo con altura l , base b e hipotenusa h . Necesitamos encontrar el área del cuadrado más grande que cabe en el triángulo rectángulo.

Ejemplos: 

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

Considerando el diagrama anterior, vemos, tanx = l/b
Aquí también es cierto que, tanx = a/(ba)
Entonces, l/b = a/(ba) lo que significa que, a = (l*b)/(l+b)

A continuación se muestra la implementación requerida:  

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>
Producción: 

12.4567

 

Complejidad del tiempo: O(1)

complejidad del espacio: O(1)

Publicación traducida automáticamente

Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *