ปริมาตรทรงกลม = ความจุของทรงกลม
สูตร: V = (4/3) × π × r³
อัลกอริทึม
Step 1: รับรัศมี (radius) จากผู้ใช้
Step 2: คำนวณ Volume = (4.0/3.0) * 3.1415 * r * r * r
Step 3: แสดงผล
โปรแกรมที่ 1: รัศมีคงที่
#include <stdio.h>
int main() {
float vol;
int rad = 20;
vol = ((4.0f/3.0f) * 3.1415 * rad * rad * rad);
printf("The volume of a sphere is %f\n", vol);
return 0;
}
ผลลัพธ์
The volume of a sphere is 33509.335938
โปรแกรมที่ 2: คำนวณพื้นที่ผิวและปริมาตร (ใช้ math.h)
#include <stdio.h>
#include <math.h>
int main() {
float rad, area, vol;
printf("Enter radius of the sphere: ");
scanf("%f", &rad);
area = 4 * (22.0/7.0) * rad * rad;
vol = (4.0/3.0) * (22.0/7.0) * rad * rad * rad;
printf("Surface area of sphere: %.3f\n", area);
printf("Volume of sphere: %.3f\n", vol);
return 0;
}
ผลลัพธ์ (radius = 4)
Surface area of sphere: 192.000
Volume of sphere: 256.000