สำหรับปริมาตรและพื้นที่ผิวของทรงกลม ขั้นแรกให้ประกาศตัวแปรที่มีค่ารัศมี
int r = 15;
หาปริมาตรของทรงกลม
// calculating volume of sphere cal_volume = (4.0 / 3) * (22 / 7) * r * r * r;
ตอนนี้คำนวณพื้นที่ผิวของทรงกลมแล้ว -
cal_area = 4 * (22 / 7) * r * r;
ให้เราดูรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
double cal_area, cal_volume, r;
// radius
r = 15;
// calculating area of sphere
cal_area = 4 * (22 / 7) * r * r;
// calculating volume of sphere
cal_volume = (4.0 / 3) * (22 / 7) * r * r * r;
Console.WriteLine("Area of Sphere: " + cal_area);
Console.WriteLine("Volume of Sphere: " + cal_volume);
}
} ผลลัพธ์
Area of Sphere: 2700 Volume of Sphere: 13500