Program to find the Area and Perimeter of a Semicircle
Last Updated :
21 Aug, 2022
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++
#include <iostream>
using namespace std;
float area( float r)
{
return (0.5)*(3.14)*(r * r);
}
float perimeter( float r)
{
return (3.14)*(r);
}
int main()
{
int r = 10;
cout << "The Area of Semicircle: "
<< area(r) << endl;
cout << "The Perimeter of Semicircle: "
<< perimeter(r) << endl;
return 0;
}
|
C
#include <stdio.h>
float area( float r)
{
return (0.5)*(3.14)*(r * r);
}
float perimeter( float r)
{
return (3.14)*(r);
}
int main()
{
float r = 10;
printf ( "The Area of Semicircle: %f\n" ,
area(r));
printf ( "The Perimeter of Semicircle: %f\n" ,
perimeter(r));
return 0;
}
|
Java
import java.io.*;
class GFG {
static float area( float r)
{
return ( float )(( 0.5 )*( 3.14 )*(r * r));
}
static float perimeter( float r)
{
return ( float )(( 3.14 )*(r));
}
public static void main (String[] args) {
float r = 10 ;
System.out.println( "The Area of Semicircle: " +
area(r));
System.out.println( "The Perimeter of Semicircle:" +
+perimeter(r));
}
}
|
Python3
def area(r):
return ( 0.5 ) * ( 3.14 ) * (r * r)
def perimeter(r):
return ( 3.14 ) * (r)
if __name__ = = '__main__' :
r = 10
print ( "The Area of Semicircle: "
,area(r))
print ( "The Perimeter of Semicircle: "
,perimeter(r))
|
C#
using System;
class GFG {
static float area( float r)
{
return ( float )((0.5)*(3.14)*(r * r));
}
static float perimeter( float r)
{
return ( float )((3.14)*(r));
}
public static void Main()
{
float r = 10;
Console.WriteLine( "The Area of Semicircle: " +
area(r));
Console.WriteLine( "The Perimeter of Semicircle:" +
perimeter(r));
}
}
|
PHP
<?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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...