Length of longest rod that can fit into a cuboid

09 Nov, 2021
Given the length, breadth, and height of a cuboid, the task is to find the length of the longest rod that can fit in a cuboid.Examples:  
Input: length = 12, breadth = 9, height = 8
Output: 17

Input: length = 22, breadth = 19, height = 8
Output: 30.1496
 
 
Explanation: In the figure  
GH = length, GF = breadth, FB= height
The length of the longest rod is BH. Therefore to compute BH we can apply Pythagoras Theorem in Triangle BHF. From triangle HGF we can compute the length of FH. 
After getting the length of FH we can find the length of BH using Pythagoras theorem in triangle BHF. 
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the longest rod// that can fit in a cuboid#include <bits/stdc++.h>using namespace std; // Function to find the lengthdouble longestRodInCuboid(int length,                        int breadth, int height){    double result;    int temp;     // temporary variable to hold    // the intermediate result    temp = length * length + breadth * breadth           + height * height;     // length of longest rod is calculated    // using square root function    result = sqrt(temp);     return result;} // Driver codeint main(){    int length = 12, breadth = 9, height = 8;     // calling longestRodInCuboid() function to    // get the length of longest rod    cout << longestRodInCuboid(length, breadth, height);     return 0;}
// Java program to find the longest// rod that can fit in a cuboidclass GFG{ // Function to find the lengthstatic double longestRodInCuboid(int length,                                 int breadth,                                 int height){    double result;    int temp;         // temporary variable to hold    // the intermediate result    temp = length * length + breadth *           breadth + height * height;         // length of longest rod is calculated    // using square root function    result = Math.sqrt(temp);         return result;} // Driver Codepublic static void main(String[] args){    int length = 12,        breadth = 9,        height = 8;     // calling longestRodInCuboid()    // function to get the length    // of longest rod    System.out.println((int)longestRodInCuboid(length,                                    breadth, height));}} // This code is contributed by ChitraNayal
# Python 3 program to find the longest rod# that can fit in a cuboid # from math lib. import everythingfrom math import * # Function to find the lengthdef longestRodInCuboid(length, breadth, height) :     # temporary variable to hold    # the intermediate result    temp = length * length + breadth  * breadth + height * height     #  length of longest rod is calculated    # using square root function    result = sqrt(temp)     return result  # Driver Codeif __name__ == "__main__" :     length, breadth, height = 12, 9, 8     # calling longestRodInCuboid() function to    # get the length of longest rod    print(longestRodInCuboid(length, breadth, height))  # This code is contributed by ANKITRAI1
// C# program to find the longest// rod that can fit in a cuboidusing System;class GFG{ // Function to find the lengthstatic double longestRodInCuboid(int length,                                int breadth,                                int height){    double result;    int temp;         // temporary variable to hold    // the intermediate result    temp = length * length + breadth *        breadth + height * height;         // length of longest rod is calculated    // using square root function    result = Math.Sqrt(temp);         return result;} // Driver Codepublic static void Main(){    int length = 12,        breadth = 9,        height = 8;     // calling longestRodInCuboid()    // function to get the length    // of longest rod    Console.WriteLine((int)longestRodInCuboid(length,                                    breadth, height));}} // This code is contributed by inder_verma..
<?php// PHP program to find the longest// rod that can fit in a cuboid // Function to find the lengthfunction longestRodInCuboid($length,                            $breadth,                            $height){    $result;    $temp;     // temporary variable to hold    // the intermediate result    $temp = $length * $length +            $breadth * $breadth +            $height * $height;     // length of longest rod is    // calculated using square    // root function    $result = sqrt($temp);     return $result;} // Driver code $length = 12; $breadth = 9;$height = 8; // calling longestRodInCuboid()// function to get the length// of longest rodecho longestRodInCuboid($length,                        $breadth, $height); // This code is contributed// by inder_verma..?>
<script> // JavaScript program to find the longest rod// that can fit in a cuboid // Function to find the lengthfunction longestRodInCuboid(length, breadth, height){    let result;    let temp;     // temporary variable to hold    // the intermediate result    temp = length * length + breadth * breadth        + height * height;     // length of longest rod is calculated    // using square root function    result = Math.sqrt(temp);     return result;} // Driver code     let length = 12, breadth = 9, height = 8;     // calling longestRodInCuboid() function to    // get the length of longest rod    document.write(longestRodInCuboid(length, breadth,                                      height));  // This code is contributed by Surbhi Tyagi. </script>
17
 
Time Complexity: O(1)
Auxiliary Space: O(1)
ankthon
ukasp
inderDuMCA
surbhityagi15
rohitkumarsinghcna
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum Manhattan distance between a distinct pair from N coordinates
Total area of two overlapping rectangles
Equation of circle when three points on the circle are given
Orientation of 3 ordered points
Program to find line passing through 2 Points
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays