Kotlin 程序查找正方形面积


在本文中,我们将了解如何查找正方形的面积。正方形的面积使用以下公式计算。

side*side

以下是相同的演示 −

假设我们的输入是 −

Length of the side : 4

所需的输出 −

Area of the square : 16

算法

  • 步骤 1 − 开始。

  • 步骤 2 − 声明 2 个整数值,即 sideValue 和 myResult。

  • 步骤 3 − 定义值。

  • 步骤 4 − 使用公式 side*side 计算正方形的面积,并存储结果。

  • 步骤 5 − 显示结果。

  • 步骤 6 − 停止。

示例 1

在此示例中,我们将使用其边长(如上述公式所示)查找正方形的面积。首先,声明并初始化一个边长变量。

val sideValue = 4

然后,查找正方形的面积 −

val myResult = sideValue * sideValue

让我们现在看一个完整的示例以查找正方形的面积 −

fun main() { val sideValue = 4 println("The length of side of the square is defined as $sideValue") val myResult = sideValue * sideValue println("The area of square is: $myResult") }

输出

The length of side of the square is defined as 4
The area of square is: 16

示例 2

在此示例中,我们将查找正方形的面积。

fun main() { val sideValue = 4 println("The length of side of the square is defined as $sideValue") squareArea(sideValue) } fun squareArea(sideValue: Int) { val myResult = sideValue * sideValue println("The area of square is: $myResult") }

输出

The length of side of the square is defined as 4
The area of square is: 16

更新于: 17-Oct-2022

606 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告