C++ 中金字塔體積程式


根據金字塔底面的型別給出各個邊,任務是計算金字塔的體積。

金字塔是一種三維圖形,其外表面呈三角形,在公共點處相交,形成金字塔的尖銳邊緣。金字塔體積取決於底面的型別。

金字塔可以由不同型別的底面構成,例如:

三角形 - 表示金字塔具有三角形底面,則金字塔體積將是

Formula - : (1/6) * a * b * h

正方形 - 表示金字塔具有正方形底面,則金字塔體積將是

Formula - : (1/3) * (b^2) * h

五邊形 - 表示金字塔具有五邊形底面,則金字塔體積將是

formula - : (5/6) * a * b * h

六邊形 - 表示金字塔具有六邊形底面,則金字塔體積將是

formula - : a * b * h

示例

Input-: a=4 b=2 h=10
Output-: Volume of pyramid with triangular base is 13.328
   Volume of pyramid with square base is 13.2
   Volume of pyramid with pentagonal base is 66.4
   Volume of pyramid with hexagonal base is 80

以下是具有正方形底面的金字塔

演算法

Start
Step 1 -> Declare function to find the volume of triangular pyramid
   float volumeTriangular(int a, int b, int h)
      Declare variable float volume = (0.1666) * a * b * h
      return volume
step 2 -> Declare Function to find the volume of square pyramid
   float volumeSquare(int b, int h)
      declare and set float volume = (0.33) * b * b * h
      return volume
Step 3 -> Declare Function to find the volume of pentagonal pyramid
   float volumePentagonal(int a, int b, int h)
      declare and set float volume = (0.83) * a * b * h
      return volume
Step 4 -> Declare Function to find the volume of hexagonal pyramid
   float volumeHexagonal(int a, int b, int h)
      declare and set float volume = a * b * h
      return volume
Step 5 -> In main()
   Declare variables as int b = 2, h = 10, a = 4
   Call volumeTriangular(a, b, h)
   Call volumeSquare(b,h)
   Call volumePentagonal(a, b, h)
   Call volumeHexagonal(a, b, h)
Stop

示例

#include <bits/stdc++.h>
using namespace std;

// Function to find the volume of triangular pyramid
float volumeTriangular(int a, int b, int h){
   float volume = (0.1666) * a * b * h;
   return volume;
}
// Function to find the volume of square pyramid
float volumeSquare(int b, int h){
   float volume = (0.33) * b * b * h;
   return volume;
}
// Function to find the volume of pentagonal pyramid
float volumePentagonal(int a, int b, int h){
   float volume = (0.83) * a * b * h;
   return volume;
}
// Function to find the volume of hexagonal pyramid
float volumeHexagonal(int a, int b, int h){
   float volume = a * b * h;
   return volume;
}
int main(){
   int b = 2, h = 10, a = 4;
   cout << "Volume of pyramid with triangular base is "<<volumeTriangular(a, b, h)<<endl;
   cout << "Volume of pyramid with square base is "<<volumeSquare(b, h)<< endl;
   cout << "Volume of pyramid with pentagonal base is "<<volumePentagonal(a, b, h)<< endl;
   cout << "Volume of pyramid with hexagonal base is "<<volumeHexagonal(a, b, h);
   return 0;
}

輸出

Volume of pyramid with triangular base is 13.328
Volume of pyramid with square base is 13.2
Volume of pyramid with pentagonal base is 66.4
Volume of pyramid with hexagonal base is 80

更新時間:2019 年 9 月 20 日

812 次檢視

開啟您的 職業生涯

完成課程以獲得認證

開始學習
廣告