Skip to content

PHP Programs 2

Write a program to find area of following shapes: circle, rectangle, triangle,
square, trapezoid and parallelogram.

<?php
    function square($var) {
        return $var * $var;
    }
    function equilateral_triangle($side) {
        return 0.43301 * ($side * $side);
    }
    function rectangle($vala, $valb) {
        return $vala * $valb;
    }
    function parallelogram($base, $height) {
        return $base * $height;
    }
    function triangle($base, $height) {
        return 0.5 * $base * $height;
    }
    function circle($radius) { 
    return 3.14 * ($radius * $radius); 
    }
    function ellipse($radius1, $radius2) {
        return 3.14 * $radius1 * $radius2;
    }
    function trapezoid($height, $base1, $base2) {
        return (($base1 + $base2) / 2) * $height;
    }
    
    echo square(4); // 16
    echo "<br>";
    echo equilateral_triangle(4); // 6.92816
    echo "<br>";
    echo circle(15); // 706.5
    echo "<br>";
    echo rectangle(15,15); // 225
    echo "<br>";
    echo parallelogram(15,5); // 75
    echo "<br>";
    echo triangle(15,5); // 37.5
    echo "<br>";
    echo ellipse(15,5); // 235.5
    echo "<br>";
    echo trapezoid(10,10,10); // 100

?>

Output