Program to calculate the area between two Concentric Circles
Last Updated :
23 Jun, 2022
Given two concentric circles with radius X and Y where (X > Y). Find the area between them.
You are required to find the area of the green region as shown in the following image:
Examples:
Input : X = 2, Y = 1
Output : 9.42478
Input : X = 4, Y = 2
Output : 37.6991
Approach:
The area between the two given concentric circles can be calculated by subtracting the area of the inner circle from the area of the outer circle. Since X>Y. X is the radius of the outer circle.
Therefore, area between the two given concentric circles will be:
?*X2 - ?*Y2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double calculateArea( int x, int y)
{
double pi = 3.1415926536;
double arx = pi * x * x;
double ary = pi * y * y;
return arx - ary;
}
int main()
{
int x = 2;
int y = 1;
cout << calculateArea(x, y);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static double calculateArea( int x, int y)
{
double pi = 3.1415926536 ;
double arx = pi * x * x;
double ary = pi * y * y;
return arx - ary;
}
public static void main (String[] args)
{
int x = 2 ;
int y = 1 ;
System.out.println (calculateArea(x, y));
}
}
|
Python3
def calculateArea(x, y):
pi = 3.1415926536
arx = pi * x * x
ary = pi * y * y
return arx - ary
x = 2
y = 1
print (calculateArea(x, y))
|
C#
using System;
class GFG
{
static double calculateArea( int x, int y)
{
double pi = 3.1415926536;
double arx = pi * x * x;
double ary = pi * y * y;
return arx - ary;
}
public static void Main ()
{
int x = 2;
int y = 1;
Console.WriteLine(calculateArea(x, y));
}
}
|
PHP
<?php
function calculateArea( $x , $y )
{
$pi = 3.1415926536;
$arx = $pi * $x * $x ;
$ary = $pi * $y * $y ;
return ( $arx - $ary );
}
$x = 2;
$y = 1;
echo calculateArea( $x , $y );
?>
|
Javascript
<script>
function calculateArea(x, y)
{
var pi = 3.1415926536;
var arx = pi * x * x;
var ary = pi * y * y;
return arx - ary;
}
var x = 2;
var y = 1;
document.write(calculateArea(x, y));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...