Open In App
Related Articles

Length of longest rod that can fit into a cuboid

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Follow the below steps to implement the above idea:

  • Declare a double variable named result to hold the length of the longest rod.
  • Declare an integer variable named temp to hold the intermediate result.
  • Calculate the intermediate result by adding the square of the length, breadth, and height values.
  • Calculate the length of the longest rod by finding the square root of the intermediate result, and assign it to the result variable.
  • Return the value of result.

Below is the implementation of the above approach:

C++

// C++ program to find the longest rod
// that can fit in a cuboid
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the length
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 = sqrt(temp);
 
    return result;
}
 
// Driver code
int 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

// Java program to find the longest
// rod that can fit in a cuboid
class GFG
{
 
// Function to find the length
static 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 Code
public 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

# Python 3 program to find the longest rod
# that can fit in a cuboid
 
# from math lib. import everything
from math import *
 
# Function to find the length
def 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 Code
if __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#

// C# program to find the longest
// rod that can fit in a cuboid
using System;
class GFG
{
 
// Function to find the length
static 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 Code
public 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
// PHP program to find the longest
// rod that can fit in a cuboid
 
// Function to find the length
function 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 rod
echo longestRodInCuboid($length,
                        $breadth, $height);
 
// This code is contributed
// by inder_verma..
?>

                    

Javascript

<script>
 
// JavaScript program to find the longest rod
// that can fit in a cuboid
 
// Function to find the length
function 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>

                    

Output: 
17

 

Time Complexity: O(log n), as the inbuilt sqrt function takes logarithmic time
Auxiliary Space: O(1)



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads