如何使用C#求解球體的體積和表面積?


對於球體的體積和表面積,首先宣告一個帶半徑值的變數。

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

更新日期: 22-Jun-2020

273 次瀏覽

開始您的職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.