📜  金字塔体积程序

📅  最后修改于: 2021-10-23 08:17:49             🧑  作者: Mango

金字塔是通过将多边形的所有角连接到中心顶点而形成的 3 维几何形状。
金字塔有很多种。大多数情况下,它们以它们所具有的碱基类型命名。下面让我们看看一些常见的金字塔类型。

以下是计算金字塔体积的代码:

C++
// CPP program to find the volume.
#include 
using namespace std;
 
// Function to find the volume
// of triangular pyramid
float volumeTriangular(int a,
                       int b,
                       int h)
{
    float vol = (0.1666) * a *
                       b * h;
    return vol;
}
 
// Function to find the
// volume of square pyramid
float volumeSquare(int b, int h)
{
    float vol = (0.33) * b *
                     b * h;
    return vol;
}
 
// Function to find the volume
// of pentagonal pyramid
float volumePentagonal(int a,
                       int b,
                       int h)
{
    float vol = (0.83) * a * b * h;
    return vol;
}
 
// Function to find the volume
// of hexagonal pyramid
float volumeHexagonal(int a,
                      int b,
                      int h)
{
    float vol = a * b * h;
    return vol;
}
 
// Driver Code
int main()
{
    int b = 4, h = 9, a = 4;
    cout << "Volume of triangular"
         << " base pyramid is "
         << volumeTriangular(a, b, h)
         << endl;
 
    cout << "Volume of square "
         << " base pyramid is "
         << volumeSquare(b, h)
         << endl;
 
    cout << "Volume of pentagonal"
         << " base pyramid is "
         << volumePentagonal(a, b, h)
         << endl;
 
    cout << "Volume of Hexagonal"
         << " base pyramid is "
         << volumeHexagonal(a, b, h);
    return 0;
}


Java
// Java Program for volume
// of Pyramid.
import java.util.*;
import java.lang.*;
 
class GfG
{
     
    // Function to find the volume of
    // triangular pyramid
    public static float volumeTriangular(int a,
                                         int b,
                                         int h)
    {
        float vol = (float)(0.1666) * a * b * h;
        return vol;
    }
 
    // Function to find the volume
    // of square pyramid
    public static float volumeSquare(int b,
                                     int h)
    {
        float vol = (float)(0.33) * b * b * h;
        return vol;
    }
 
    // Function to find the volume of
    // pentagonal pyramid
    public static float volumePentagonal(int a,
                                         int b,
                                         int h)
    {
        float vol = (float)(0.83) * a * b * h;
        return vol;
    }
 
    // Function to find the volume of hexagonal
    // pyramid
    public static float volumeHexagonal(int a,
                                        int b,
                                        int h)
    {
        float vol = (float)a * b * h;
        return vol;
    }
     
    // Driver Code
    public static void main(String argc[])
    {
        int b = 4, h = 9, a = 4;
        System.out.println("Volume of triangular"+
                             " base pyramid is " +
                       volumeTriangular(a, b, h));
 
        System.out.println("Volume of square base" +
                                    " pyramid is " +
                                volumeSquare(b, h));
 
        System.out.println("Volume of pentagonal"+
                             " base pyramid is " +
                       volumePentagonal(a, b, h));
 
        System.out.println("Volume of Hexagonal"+
                              " base pyramid is " +
                         volumeHexagonal(a, b, h));
    }
}
 
// This code is contributed by Sagar Shukla


Python3
# Python3 program to Volume of Pyramid
 
# Function to calculate
# Volume of Triangular Pyramid
def volumeTriangular(a, b, h):
    return (0.1666) * a * b * h
 
# Function To calculate
# Volume of Square Pyramid
def volumeSquare(b, h):
    return (0.33) * b * b * h
 
# Function To calculate Volume
# of Pentagonal Pyramid
def volumePentagonal(a, b, h):
    return (0.83) * a * b * h
 
# Function To calculate Volume
# of Hexagonal Pyramid
def volumeHexagonal(a, b, h):
    return a * b * h
 
 
# Driver Code
b = float(4)
h = float(9)
a = float(4)
print( "Volume of triangular base pyramid is ",
                    volumeTriangular(a, b, h) )
print( "Volume of square base pyramid is ",
                    volumeSquare(b, h) )
print( "Volume of pentagonal base pyramid is ",
                    volumePentagonal(a,b, h) )
print( "Volume of Hexagonal base pyramid is ",
                    volumeHexagonal(a, b, h))
 
# This code is contributed by rishabh_jain


C#
// C# Program for volume of Pyramid.
using System;
 
class GFG
{
     
    // Function to find the volume of
    // triangular pyramid
    public static float volumeTriangular(int a,
                                         int b,
                                         int h)
    {
        float vol = (float)(0.1666) * a * b * h;
        return vol;
    }
 
    // Function to find the volume
    // of square pyramid
    public static float volumeSquare(int b,
                                     int h)
    {
        float vol = (float)(0.33) * b * b * h;
        return vol;
    }
 
    // Function to find the volume 
    // of pentagonal pyramid
    public static float volumePentagonal(int a,
                                         int b,
                                         int h)
    {
        float vol = (float)(0.83) * a * b * h;
        return vol;
    }
 
    // Function to find the volume
    // of hexagonal pyramid
    public static float volumeHexagonal(int a,
                                        int b,
                                        int h)
    {
        float vol = (float)a * b * h;
        return vol;
    }
     
    // Driver Code
    public static void Main()
    {
        int b = 4, h = 9, a = 4;
        Console.WriteLine("Volume of triangular"+
                            " base pyramid is " +
                      volumeTriangular(a, b, h));
 
        Console.WriteLine("Volume of square "+
                          "base pyramid is " +
                          volumeSquare(b, h));
 
        Console.WriteLine("Volume of pentagonal"+
                            " base pyramid is " +
                      volumePentagonal(a, b, h));
 
        Console.WriteLine("Volume of Hexagonal"+
                           " base pyramid is " +
                      volumeHexagonal(a, b, h));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出 :

Volume of triangular base pyramid is 23.9904
Volume of square base pyramid is 47.52
Volume of pentagonal base pyramid is 119.52
Volume of Hexagonal base pyramid is 144