Length of longest rod that can fit into a cuboid
Last Updated :
14 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
double longestRodInCuboid( int length,
int breadth, int height)
{
double result;
int temp;
temp = length * length + breadth * breadth
+ height * height;
result = sqrt (temp);
return result;
}
int main()
{
int length = 12, breadth = 9, height = 8;
cout << longestRodInCuboid(length, breadth, height);
return 0;
}
|
Java
class GFG
{
static double longestRodInCuboid( int length,
int breadth,
int height)
{
double result;
int temp;
temp = length * length + breadth *
breadth + height * height;
result = Math.sqrt(temp);
return result;
}
public static void main(String[] args)
{
int length = 12 ,
breadth = 9 ,
height = 8 ;
System.out.println(( int )longestRodInCuboid(length,
breadth, height));
}
}
|
Python 3
from math import *
def longestRodInCuboid(length, breadth, height) :
temp = length * length + breadth * breadth + height * height
result = sqrt(temp)
return result
if __name__ = = "__main__" :
length, breadth, height = 12 , 9 , 8
print (longestRodInCuboid(length, breadth, height))
|
C#
using System;
class GFG
{
static double longestRodInCuboid( int length,
int breadth,
int height)
{
double result;
int temp;
temp = length * length + breadth *
breadth + height * height;
result = Math.Sqrt(temp);
return result;
}
public static void Main()
{
int length = 12,
breadth = 9,
height = 8;
Console.WriteLine(( int )longestRodInCuboid(length,
breadth, height));
}
}
|
PHP
<?php
function longestRodInCuboid( $length ,
$breadth ,
$height )
{
$result ;
$temp ;
$temp = $length * $length +
$breadth * $breadth +
$height * $height ;
$result = sqrt( $temp );
return $result ;
}
$length = 12; $breadth = 9;
$height = 8;
echo longestRodInCuboid( $length ,
$breadth , $height );
?>
|
Javascript
<script>
function longestRodInCuboid(length, breadth, height)
{
let result;
let temp;
temp = length * length + breadth * breadth
+ height * height;
result = Math.sqrt(temp);
return result;
}
let length = 12, breadth = 9, height = 8;
document.write(longestRodInCuboid(length, breadth,
height));
</script>
|
Time Complexity: O(log n), as the inbuilt sqrt function takes logarithmic time
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...