Given the radius of the semicircle as r, the task is to find out the Area and Perimeter of that semicircle. Examples:
Input: r = 10
Output: Area = 157.00, Perimeter = 31.4
Input: r = 25
Output: Area =981.250000, Perimeter = 78.500000
Approach: In mathematics, a semicircle is a one-dimensional locus of points that forms half of a circle. The area of a semicircle is half the area of the circle from which it is made. Any diameter of a circle cuts it into two equal semicircles.

Area of Semi-Circle = 1?2 * ? *r2 Perimeter of Semi-Circle = ? *r where “r” is the radius of the semicircle.
Below is the implementation of the above approach:
C++
<?php
function area( $r )
{
return (0.5) * (3.14) * ( $r * $r );
}
function perimeter( $r )
{
return (3.14) * ( $r );
}
$r = 10;
echo "The Area of Semicircle: " ,
area( $r ), "\n" ;
echo "The Perimeter of Semicircle: " ,
perimeter( $r ), "\n" ;
?>
|
Javascript
<script>
function area(r) {
return ((0.5) * (3.14) * (r * r));
}
function perimeter(r) {
return ((3.14) * (r));
}
var r = 10;
document.write( "The Area of Semicircle: " + area(r).toFixed(6)+ "<br/>" );
document.write( "The Perimeter of Semicircle: " +
perimeter(r).toFixed(6)+ "<br/>" );
</script>
|
Output:
The Area of Semicircle: 157.000000
The Perimeter of Semicircle: 31.400000
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
|