Kotlin程式查詢圓的周長


在本文中,我們將瞭解如何求圓的周長。圓的周長由公式計算。

(2*22*radius)/7

你也可以這樣寫表示式−

2ℼr

ℼ 的值為 22/7 或 3.14。

假設我們的輸⼊是 −

Radius of the circle : 5

所需的輸出來看 −

Perimeter of Circle is: 31.428571428571427

演算法

  • Step 1 − START。

  • Step 2 − 宣告 2 個 double 型別的數值,分別是半徑和 myResult。

  • 步驟 3 − 定義值。

  • Step 4 − 使用公式 (2*22*半徑)/7 計算周長,並存儲結果。

  • 步驟 5 − 顯示結果。

  • Step 6 − 停止。

示例 1

在這個示例中,我們將找到圓的周長。首先,宣告並初始化一個變數,用於表示圓的半徑 −

val radius = 5

現在,在新的變數 myResult 中,使用上述公式計算周長 −

val myResult = (2 * 22 * radius)/7

現在讓我們看看完整的示例,以求出圓的周長 −

fun main() { val radius = 5 println("The radius of the circle is defined as $radius") val myResult = (2 * 22 * radius)/7 println("The perimeter of the circle is: $myResult") }

輸出

The radius of the circle is defined as 5
The perimeter of the circle is: 31

示例 2

在此示例中,我們將找到圓的周長。

fun main() { val radius = 5 println("The radius of the circle is defined as $radius") circlePerimeter(radius) } fun circlePerimeter(radius: Int) { val myResult = (2 * 22 * radius)/7 println("The perimeter of the circle is: $myResult") }

輸出

The radius of the circle is defined as 5
The perimeter of the circle is: 31

更新時間: 17-Oct-2022

402 瀏覽

開啟你的職業生涯 之路

透過完成課程獲得認證

開始
廣告
© . All rights reserved.