C++程式:計算兩個同心圓之間的面積?


什麼是同心圓?

同心圓是指內含於另一個圓內的圓,這意味著它們共享同一個圓心,但半徑長度不同,即r1和r2,其中r2>r1。兩個同心圓之間的區域稱為環形區域。

下圖是同心圓的圖形

問題

給定兩個半徑分別為r1和r2的不同同心圓,其中r2>r1。任務是找到兩個圓之間的面積,該面積用藍色突出顯示。

要計算兩個圓之間的面積,我們可以從較大的圓的面積中減去較小的圓的面積。

假設,較大的圓的半徑為r2,較小的圓的半徑為r1,則

示例

Input-: r1=3 r2=4
Output-: area between two given concentric circle is :21.98

演算法

Start
Step 1 -> define macro as
   #define pi 3.14
Step 2 -> Declare function to find area between the two given concentric circles
   double calculateArea(int x, int y)
   set double outer = pi * x * x
   Set double inner = pi * y * y
return outer-inner
step 3 -> In main()
   Declare variable as int x = 4 and int y = 3
   Print calculateArea(x,y)
Stop

示例

#include <bits/stdc++.h>
#define pi 3.14
using namespace std;
// Function to find area between the two given concentric circles
double calculateArea(int x, int y){
   double outer = pi * x * x;
   double inner = pi * y * y;
   return outer-inner;
}
int main(){
   int x = 4;
   int y = 3;
   cout <<"area between two given concentric circle is :"<<calculateArea(x, y);
   return 0;
}

輸出

area between two given concentric circle is :21.98

更新於:2019年9月20日

379 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.