Longest rod that can be inserted within a right circular cylinder
Given a right circular cylinder of height
, & radius
. The task is to find the length of the longest rod that can be inserted within it.
Examples:
Input : h = 4, r = 1.5 Output : 5 Input : h= 12, r = 2.5 Output : 13

Approach:
From the figure, it is clear that we can get the length of the rod by using pythagoras theorem, by treating the height of cylinder as perpendicular, diameter as base and length of rod as hypotenuse.
So, l2 = h2 + 4*r2.
Therefore,
l = √(h2 + 4*r2)
Below is the implementation of the above approach:
C++
// C++ Program to find the longest rod// that can be fit within a right circular cylinder#include <bits/stdc++.h>using namespace std;// Function to find the side of the cubefloat rod(float h, float r){ // height and radius cannot be negative if (h < 0 && r < 0) return -1; // length of rod float l = sqrt(pow(h, 2) + 4 * pow(r, 2)); return l;}// Driver codeint main(){ float h = 4, r = 1.5; cout << rod(h, r) << endl; return 0;} |
Java
// Java Program to find the longest rod// that can be fit within a right circular cylinderimport java.io.*;class GFG { // Function to find the side of the cubestatic float rod(float h, float r){ // height and radius cannot be negative if (h < 0 && r < 0) return -1; // length of rod float l = (float)(Math.sqrt(Math.pow(h, 2) + 4 * Math.pow(r, 2))); return l;}// Driver code public static void main (String[] args) { float h = 4; float r = 1.5f; System.out.print(rod(h, r)); }}// This code is contributed by anuj_67.. |
Python 3
# Python 3 Program to find the longest# rod that can be fit within a right# circular cylinderimport math# Function to find the side of the cubedef rod(h, r): # height and radius cannot # be negative if (h < 0 and r < 0): return -1 # length of rod l = (math.sqrt(math.pow(h, 2) + 4 * math.pow(r, 2))) return float(l)# Driver codeh , r = 4, 1.5print(rod(h, r))# This code is contributed# by PrinciRaj1992 |
C#
// C# Program to find the longest// rod that can be fit within a// right circular cylinderusing System;class GFG{// Function to find the side// of the cubestatic float rod(float h, float r){ // height and radius cannot // be negative if (h < 0 && r < 0) return -1; // length of rod float l = (float)(Math.Sqrt(Math.Pow(h, 2) + 4 * Math.Pow(r, 2))); return l;}// Driver codepublic static void Main (){ float h = 4; float r = 1.5f; Console.WriteLine(rod(h, r));}}// This code is contributed by shs |
PHP
<?php// PHP Program to find the longest// rod that can be fit within a// right circular cylinder// Function to find the side// of the cubefunction rod($h, $r){ // height and radius cannot // be negative if ($h < 0 && $r < 0) return -1; // length of rod $l = sqrt(pow($h, 2) + 4 * pow($r, 2)); return $l;}// Driver code$h = 4; $r = 1.5;echo rod($h, $r) . "\n";// This code is contributed// by Akanksha Rai?> |
Javascript
<script> // javascript Program to find the longest rod// that can be fit within a right circular cylinder// Function to find the side of the cubefunction rod(h , r){ // height and radius cannot be negative if (h < 0 && r < 0) return -1; // length of rod var l = (Math.sqrt(Math.pow(h, 2) + 4 * Math.pow(r, 2))); return l;}// Driver codevar h = 4;var r = 1.5;document.write(rod(h, r));// This code contributed by shikhasingrajput</script> |
5
Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.


