Kotlin程式:求梯形面積
在本文中,我們將瞭解如何求梯形的面積。梯形的面積使用以下公式計算。
(height/2 * (side1 + side2))
下面是演示:
假設我們的輸入是:
side1 = 5 side2 = 6 height = 6
期望輸出是:
Area of trapezium is: 33.0
演算法
步驟 1 − 開始。
步驟 2 − 宣告四個整型變數:side1、side2、height和myResult。
步驟 3 − 定義變數值。
步驟 4 − 使用公式 (height/2 * (side1 + side2)) 計算梯形的面積並存儲結果。
步驟 5 − 顯示結果。
步驟 6 − 結束。
示例 1
在這個例子中,我們將根據梯形的底和高求梯形的面積。首先宣告並初始化表示底和高的變數。
val side1 = 5 val side2 = 6 val height = 6
現在,使用上述公式求梯形的面積:
val myResult = (height/2 * (side1 + side2));
讓我們來看一個求梯形面積的例子:
fun main() { val side1 = 5 val side2 = 6 val height = 6 println("The length of sides of the trapezium are defined as $side1, $side2, $height") val myResult = (height/2 * (side1 + side2)); println("The area of square is: $myResult") }
輸出
The length of sides of the trapezium are defined as 5, 6, 6 The area of square is: 33
示例 2
在這個例子中,我們將求梯形的面積。
fun main() { val side1 = 5 val side2 = 6 val height = 6 println("The length of sides of the trapezium are defined as $side1, $side2, $height") trapeziumArea(side1, side2, height) } fun trapeziumArea(side1: Int, side2: Int, height: Int) { val myResult = (height/2 * (side1 + side2)); println("The area of square is: $myResult") }
輸出
The length of sides of the trapezium are defined as 5, 6, 6 The area of square is: 33
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP