用于查找圆的面积和周长的 PHP 程序


2023 年 10 月 21 日, Learn eTutorial
3432

如何根据给定半径查找圆的面积和周长

圆的面积是指圆形所包围的总面积。圆的周长或周界是圆曲线的总长度。

要查找圆的面积,首先,我们必须知道圆的半径并使用公式

面积 = pi r2

  • 其中 'r' 是圆的半径
  • pi 是一个常数,其值为 3.14

要查找圆的周长或周界,我们使用公式

周长 = 2 pi r。

  • r 是圆的半径
  • pi 是一个常数,其值为 3.14

例如,如果圆的半径是 7,那么圆的面积将是 π * 72 ,即 153.938。周长或周界将是 2 * π * 7 ,即 43.982。

如何使用 PHP 查找面积和周长

在此 PHP 程序中,我们使用内置函数

  • pi() 用于 pi(3.14)
  • pow() 用于查找值的幂
  • round() 用于限制小数点后的位数。

首先,我们必须从用户读取半径并将其赋值给一个变量,然后要查找面积,我们必须使用公式

面积 = pi() * pow(radius, 2) 

要查找周长或周界,我们必须使用公式

周长或周界 = 2 * pi() * radius

算法

步骤 1: 接受用户输入的半径并将其赋值给变量 radius

步骤 2: 使用公式 'pi() * pow(radius, 2)' 计算面积并将其赋值给变量 area

步骤 3: 使用公式 '2 * pi() * radius' 计算周长并将其值添加到变量 circumference

步骤 4: 打印变量 'area' 和 'circumference' 的值,作为圆的面积和周长

PHP 源代码

                                          <?php
$radius = readline("Enter the radius: ");
$area = pi() * pow($radius, 2);      // pow() is used to find the power
$area = round($area, 3);            // round() is used to limit the number of digit after the decimal
$circumference = 2 * pi() * $radius;
$circumference = round($circumference, 3);
echo (" Area of circle is $area \n Circumference of the circle is $circumference");
?>
                                      

输出

Enter the radius: 15
Area of circle is 706.858
Circumference of the circle is 94.248