Saturday, October 5, 2024
Google search engine
HomeData Modelling & AIProgram to find the Area and Perimeter of a Semicircle

Program to find the Area and Perimeter of a Semicircle

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++




// C++ program to find the
// Area and Perimeter of a Semicircle
 
#include <iostream>
using namespace std;
 
// Function for calculating the area
float area(float r)
{
    // Formula for finding the area
    return (0.5)*(3.14)*(r * r);
}
 
// Function for calculating the perimeter
float perimeter(float r)
{
    // Formula for finding the perimeter
    return (3.14)*(r);
}
 
// driver code
int main()
{
 
    // Get the radius
    int r = 10;
 
    // Find the area
    cout << "The Area of Semicircle: "
         << area(r) << endl;
 
    // Find the perimeter
    cout << "The Perimeter of Semicircle: "
         << perimeter(r) << endl;
 
    return 0;
}


C




// C program to find the
// Area and Perimeter of a Semicircle
 
#include <stdio.h>
 
// Function for calculating the area
float area(float r)
{
    // Formula for finding the area
    return (0.5)*(3.14)*(r * r);
}
 
// Function for calculating the perimeter
float perimeter(float r)
{
    // Formula for finding the perimeter
    return (3.14)*(r);
}
 
// driver code
int main()
{
 
    // Get the radius
    float r = 10;
 
    // Find the area
    printf("The Area of Semicircle: %f\n",
        area(r));
 
    // Find the perimeter
    printf("The Perimeter of Semicircle: %f\n",
        perimeter(r));
    return 0;
}


Java




// Java program to find the
// Area and Perimeter of a Semicircle
 
import java.io.*;
 
class GFG {
 
// Function for calculating the area
static float area(float r)
{
    // Formula for finding the area
    return (float)((0.5)*(3.14)*(r * r));
}
 
// Function for calculating the perimeter
static float perimeter(float r)
{
    // Formula for finding the perimeter
    return (float)((3.14)*(r));
}
 
// driver code
 
    public static void main (String[] args) {
    // Get the radius
    float r = 10;
 
    // Find the area
    System.out.println("The Area of Semicircle: "+
        area(r));
 
    // Find the perimeter
    System.out.println("The Perimeter of Semicircle:"+
        +perimeter(r));
    }
}
 // This code is contributed
// by anuj_67..


Python3




# Python3 program to find the
# Area and Perimeter of a Semicircle
 
# Function for calculating the area
def area(r):
     
    # Formula for finding the area
    return (0.5)*(3.14)*(r * r)
 
#Function for calculating the perimeter
def perimeter(r):
     
    #Formula for finding the perimeter
    return (3.14)*(r)
 
# driver code
if __name__=='__main__':
    # Get the radius
    r = 10
 
    # Find the area
    print ("The Area of Semicircle: "
           ,area(r))
 
    # Find the perimeter
    print ("The Perimeter of Semicircle: "
           ,perimeter(r))
            
# This code is contributed by
# SURENDRA_GANGWAR


C#




// C# program to find the
// Area and Perimeter of a Semicircle
using System;
 
class GFG {
 
// Function for calculating the area
static float area(float r)
{
    // Formula for finding the area
    return (float)((0.5)*(3.14)*(r * r));
}
 
// Function for calculating the perimeter
static float perimeter(float r)
{
    // Formula for finding the perimeter
    return (float)((3.14)*(r));
}
 
// Driver Code
public static void Main()
{
    // Get the radius
    float r = 10;
     
    // Find the area
    Console.WriteLine("The Area of Semicircle: " +
                                         area(r));
     
    // Find the perimeter
    Console.WriteLine("The Perimeter of Semicircle:" +
                                        perimeter(r));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP




<?php
// PHP program to find the
// Area and Perimeter of a Semicircle
 
// Function for calculating the area
function area($r)
{
    // Formula for finding the area
    return (0.5) * (3.14) * ($r * $r);
}
 
// Function for calculating
// the perimeter
function perimeter($r)
{
    // Formula for finding
    // the perimeter
    return (3.14) * ($r);
}
 
// Driver code
 
// Get the radius
$r = 10;
 
// Find the area
echo "The Area of Semicircle: ",
    area($r),"\n" ;
 
// Find the perimeter
echo "The Perimeter of Semicircle: ",
    perimeter($r),"\n" ;
 
// This code is contributed
// by ANKITRAI1
?>


Javascript




<script>
// javascript program to find the
// Area and Perimeter of a Semicircle
 
// Function for calculating the area
    function area(r) {
        // Formula for finding the area
        return  ((0.5) * (3.14) * (r * r));
    }
 
    // Function for calculating the perimeter
    function perimeter(r) {
        // Formula for finding the perimeter
        return  ((3.14) * (r));
    }
 
    // driver code
 
     
        // Get the radius
        var r = 10;
 
        // Find the area
        document.write("The Area of Semicircle: " + area(r).toFixed(6)+"<br/>");
 
        // Find the perimeter
        document.write("The Perimeter of Semicircle: " +
        perimeter(r).toFixed(6)+"<br/>");
 
// This code contributed by gauravrajput1
 
</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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments