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 次查看

开启您的 职业生涯

完成课程以获得认证

开始学习
广告