Swift程式設計計算五邊形面積
本教程將討論如何編寫Swift程式來計算五邊形的面積。
五邊形是一個二維圖形,有5條邊。它也包含五個內角,其和為540度。它也被稱為五邊形。五邊形五條邊所包圍的總空間稱為五邊形的面積。
公式
以下是五邊形面積的公式
Area = 1/4(sqrt(5(5+2√5))q2)
下面是相同的演示 -
輸入
假設我們的給定輸入為 -
side = 6
輸出
期望輸出為 -
Area of the pentagon= 61.93718642120281
演算法
以下是演算法 -
步驟1 - 建立一個具有返回值的函式。
步驟2 - 使用以下公式查詢五邊形的面積
return (sqrt(5*(5+2*sqrt(5))) * q * q)/4
步驟3 - 呼叫函式並將邊作為引數傳遞給函式。
步驟4 - 列印輸出。
示例
以下程式演示如何計算五邊形的面積。
import Foundation import Glibc // Creating a function to find the area of pentagon func pentagonArea(q:Double) -> Double{ return (sqrt(5*(5+2*sqrt(5))) * q * q)/4 } var num = 10.0 print("Length of the side is", num) print("Area of the pentagon:", pentagonArea(q:num))
輸出
Length of the side is 10.0 Area of the pentagon: 172.0477400588967
在這裡,在上面的程式中,我們建立了一個函式,它使用以下公式返回五邊形的面積:
return (sqrt(5*(5+2*sqrt(5))) * q * q)/4
在這裡,我們使用sqrt()函式來查詢平方根。
廣告