Volumen de la esfera más grande dentro de un cilindro circular recto

Dado un cilindro circular recto de radio  r   y altura  h   . La tarea es encontrar el radio de la esfera más grande que se puede inscribir en ella.
Ejemplos
 

Input : r = 4, h = 8
Output : 4

Input : r = 5, h= 10
Output :5

Enfoque : del diagrama, está claro que el radio de la esfera será claramente igual al radio de la base del cilindro.
Entonces, R = r
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ Program to find the biggest sphere
// that can be fit within a right circular cylinder
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the biggest sphere
float sph(float r, float h)
{
 
    // radius and height cannot be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of sphere
    float R = r;
    return R;
}
 
// Driver code
int main()
{
    float r = 4, h = 8;
    cout << sph(r, h) << endl;
    return 0;
}

Java

// Java Program to find the biggest
// sphere that can be fit within a
// right circular cylinder
import java.io.*;
 
class GFG
{
 
// Function to find the biggest sphere
static float sph(float r, float h)
{
 
    // radius and height cannot
    // be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of sphere
    float R = r;
    return R;
}
 
// Driver code
public static void main (String[] args)
{
    float r = 4, h = 8;
    System.out.println(sph(r, h));
}
}
 
// This code is contributed
// by inder_verma

Python3

# Python 3 Program to find the biggest
# sphere that can be fit within a right
# circular cylinder
 
# Function to find the biggest sphere
def sph(r, h):
     
    # radius and height cannot
    # be negative
    if (r < 0 and h < 0):
        return -1
 
    # radius of sphere
    R = r
    return float(R)
 
# Driver code
r, h = 4, 8
print(sph(r, h))
 
# This code is contributed
# by PrinciRaj1992

C#

// C# Program to find the biggest
// sphere that can be fit within a
// right circular cylinder
using System;
 
class GFG
{
 
// Function to find the biggest sphere
static float sph(float r, float h)
{
 
    // radius and height cannot
    // be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of sphere
    float R = r;
    return R;
}
 
// Driver code
public static void Main ()
{
    float r = 4, h = 8;
    Console.WriteLine(sph(r, h));
}
}
 
// This code is contributed
// by shs..

PHP

<?php
    // PHP Program to find the biggest sphere
// that can be fit within a right circular cylinder
 
// Function to find the biggest sphere
function sph($r, $h)
{
 
    // radius and height cannot be negative
    if ($r < 0 && $h < 0)
        return -1;
 
    // radius of sphere
    $R = $r;
    return $R;
}
 
// Driver code
 
    $r = 4 ;$h = 8;
    echo sph($r, $h);
 
// This code is contributed
// by shs..
?>

Javascript

<script>
// javascript Program to find the biggest
// sphere that can be fit within a
// right circular cylinder
 
// Function to find the biggest sphere
function sph(r , h)
{
 
    // radius and height cannot
    // be negative
    if (r < 0 && h < 0)
        return -1;
 
    // radius of sphere
    var R = r;
    return R;
}
 
// Driver code
var r = 4, h = 8;
document.write(sph(r, h));
 
// This code is contributed by shikhasingrajput
</script>
Producción: 

4

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: 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 *