如何使用 C 程式語言計算球體的體積?


球體的體積只不過是形狀的容量。

球體的體積公式如下 −

$$V\:=\:\frac{4}{3}\Pi\:r^{3}$$

演算法

Step 1: Enter radius of sphere at runtime
Step 2: Apply the formula to variable
        Volume=(4/3)*3.14*rad*rad*rad
Step 3: print the volume
Step 4: stop

程式 1

 現場演示

#include<stdio.h>
int main(){
   float vol;
   int rad;
   rad=20;
   vol=((4.0f/3.0f) * (3.1415) * rad * rad * rad);
   printf("the volume of a sphere is %f
",vol);    return 0; }

輸出

the volume of a sphere is 33509.335938

程式 2

以下是計算球體體積與表面積的一個示例 −

 現場演示

#include <stdio.h>
#include <math.h>
int main(){
   float rad;
   float area, vol;
   printf("Enter radius of the sphere : 
");    scanf("%f", &rad);    area = 4 * (22/7) * rad * rad;    vol = (4.0/3) * (22/7) * rad * rad * rad;    printf("Surface area of sphere is: %.3f", area);    printf("
Volume of sphere is : %.3f", vol);    return 0; }

輸出

Enter radius of the sphere :
4
Surface area of sphere is: 192.000
Volume of sphere is : 256.000

更新日期:2021 年 3 月 5 日

5000 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.