📜  程序计算两个同心圆之间的面积

📅  最后修改于: 2021-04-26 18:56:21             🧑  作者: Mango

给定两个半径为XY的同心圆,其中( X> Y )。找到它们之间的区域。
您需要找到绿色区域的区域,如下图所示:

例子:

Input : X = 2, Y = 1
Output : 9.42478

Input : X = 4, Y = 2
Output : 37.6991

方法:
可以通过从外圆的面积中减去内圆的面积来计算两个给定的同心圆之间的面积。由于X> Y。 X是外圆的半径。
因此,两个给定的同心圆之间的面积将为:

π*X2 - π*Y2

下面是上述方法的实现:

C++
// C++ program to find area between the
// two given concentric circles
 
#include 
using namespace std;
 
// Function to find area between the
// two given concentric circles
double calculateArea(int x, int y)
{
    // Declare value of pi
    double pi = 3.1415926536;
 
    // Calculate area of outer circle
    double arx = pi * x * x;
 
    // Calculate area of inner circle
    double ary = pi * y * y;
 
    // Difference in areas
    return arx - ary;
}
 
// Driver Program
int main()
{
    int x = 2;
    int y = 1;
 
    cout << calculateArea(x, y);
 
    return 0;
}


Java
// Java program to find area between the
// two given concentric circles
import java.io.*;
 
class GFG
{
     
// Function to find area between the
// two given concentric circles
static double calculateArea(int x, int y)
{
    // Declare value of pi
    double pi = 3.1415926536;
 
    // Calculate area of outer circle
    double arx = pi * x * x;
 
    // Calculate area of inner circle
    double ary = pi * y * y;
 
    // Difference in areas
    return arx - ary;
}
 
// Driver code
public static void main (String[] args)
{
    int x = 2;
    int y = 1;
    System.out.println (calculateArea(x, y));
}
}
 
// This code is contributed by jit_t.


Python3
# Python3 program to find area between 
# the two given concentric circles
 
# Function to find area between the
# two given concentric circles
def calculateArea(x, y):
 
    # Declare value of pi
    pi = 3.1415926536
 
    # Calculate area of outer circle
    arx = pi * x * x
 
    # Calculate area of inner circle
    ary = pi * y * y
 
    # Difference in areas
    return arx - ary
 
# Driver Code
x = 2
y = 1
 
print(calculateArea(x, y))
 
# This code is contributed
# by shashank_sharma


C#
// C# program to find area between the
// two given concentric circles
using System;
 
class GFG
{
     
// Function to find area between the
// two given concentric circles
static double calculateArea(int x, int y)
{
    // Declare value of pi
    double pi = 3.1415926536;
 
    // Calculate area of outer circle
    double arx = pi * x * x;
 
    // Calculate area of inner circle
    double ary = pi * y * y;
 
    // Difference in areas
    return arx - ary;
}
 
// Driver code
public static void Main ()
{
    int x = 2;
    int y = 1;
    Console.WriteLine(calculateArea(x, y));
}
}
 
// This code is contributed by Code_Mech.


PHP


Javascript


输出:
9.42478

时间复杂度: O(1)