PHP Practical Slip 15 Q.B

Write a PHP script to calculate the area and volume of a cylinder using a function.

<?php
// Function to calculate area and volume
function cylinder($radius, $height) {
    $area = 2 * pi() * $radius * ($radius + $height); // Surface area
    $volume = pi() * $radius * $radius * $height;     // Volume
    
    return ["area" => $area, "volume" => $volume];
}

// Example usage
$radius = 3;
$height = 7;

$result = cylinder($radius, $height);

// Display results
echo "Radius: $radius<br>";
echo "Height: $height<br>";
echo "Surface Area of Cylinder: " . round($result['area'], 2) . "<br>";
echo "Volume of Cylinder: " . round($result['volume'], 2);
?>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top