Program for Volume of Pyramid

Python Program for Volume of Pyramid

Given the base, height, area of a pyramid and the task is to calculate the volume of given pyramid types in python.

Pyramid :

A pyramid is a three-dimensional geometric shape created by connecting all of the corners of a polygon to a central apex.

Pyramids come in a variety of shapes and sizes. They are usually named after the type of base they have. Let’s take a look at some of the most common types of pyramids below.

Formulas:

The Volume of square base pyramid  = (1/3) * (b^2) * h
The volume of triangular base pyramid = (1/6) * a * b * h
The Volume of pentagonal base pyramid = (5/6) * a * b * h
The volume of a hexagonal base pyramid = a * b * h

where b= base of the pyramid

h= height

a= area

Examples:

Example1:

Input:

Given base = 3
Given height = 5
Given area = 20

Output:

The Volume of triangular base pyramid with given base, height, area { 3 , 5 , 20 } =  49.97999999999999
The Volume of square base pyramid with given base, height, area { 3 , 5 , 20 } =  14.849999999999998
The Volume of pentagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  249.0
The Volume of hexagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  300

Example2:

Input:

Given base = 4
Given height = 6.2
Given area =  15

Output:

The Volume of triangular base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 61.9752
The Volume of square base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 32.736000000000004
The Volume of pentagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 308.76
The Volume of hexagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 372.0

Program for Volume of Pyramid in Python

Below are the ways to calculate the volume of given pyramid types in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the base as static input and store it in a variable.
  • Give the height as static input and store it in another variable.
  • Give the area as static input and store it in another variable.
  • Calculate the volume of the triangular pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the square pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Print the volume of the triangular pyramid.
  • Print the volume of the square pyramid.
  • Print the volume of the pentagonal pyramid.
  • Print the volume of the hexagonal pyramid.
  • The Exit of the Program.

Below is the implementation:

# Give the base as static input and store it in a variable.
gvn_baseval = 3
# Give the height as static input and store it in another variable.
gvn_heigt = 5
# Give the area as static input and store it in another variable.
gvn_area = 20
# Calculate the volume of the triangular pyramid using the above given mathematical formula.
# Store it in another variable.
triangulr_pyramidvol = (0.1666) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the square pyramid using the above given mathematical formula.
# Store it in another variable.
squre_pyramidvol = (0.33) * gvn_baseval * gvn_baseval * gvn_heigt
# Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
# Store it in another variable.
pentagonl_pyramidvol = (0.83) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
# Store it in another variable.
hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt
# Print the volume of the triangular pyramid.
print("The Volume of triangular base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", triangulr_pyramidvol)
# Print the volume of the square pyramid.
print("The Volume of square base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", squre_pyramidvol)
# Print the volume of the pentagonal pyramid.
print("The Volume of pentagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", pentagonl_pyramidvol)
# Print the volume of the hexagonal pyramid.
print("The Volume of hexagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", hexgnl_pyramidvol)
#include <iostream>
#include<math.h>

using namespace std;

int main() {
 int gvn_baseval = 3;
int gvn_heigt = 5;
int gvn_area = 20;
double triangulr_pyramidvol = ( 0.1666 ) * gvn_area * gvn_baseval * gvn_heigt;
double squre_pyramidvol = ( 0.33 ) * gvn_baseval * gvn_baseval * gvn_heigt;
double pentagonl_pyramidvol = ( 0.83 ) * gvn_area * gvn_baseval * gvn_heigt;
 double hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt;
cout<<"The Volume of triangular base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<triangulr_pyramidvol<<endl;
cout<<"The Volume of square  base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<squre_pyramidvol<<endl;
cout<<"The Volume of pentagonal  base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<pentagonl_pyramidvol<<endl;
cout<<"The Volume of hexagonal base pyramid with given base, height, area {"<<gvn_baseval<< ","<<gvn_heigt<<","<<gvn_area<<"} = "<<hexgnl_pyramidvol<<endl;

}

Output:

The Volume of triangular base pyramid with given base, height, area { 3 , 5 , 20 } =  49.97999999999999
The Volume of square base pyramid with given base, height, area { 3 , 5 , 20 } =  14.849999999999998
The Volume of pentagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  249.0
The Volume of hexagonal base pyramid with given base, height, area { 3 , 5 , 20 } =  300

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the base as user input using the float(input()) function and store it in a variable.
  • Give the height as user input using the float(input()) function and store it in another variable.
  • Give the area as user input using the float(input()) function and store it in another variable.
  • Calculate the volume of the triangular pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the square pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
  • Store it in another variable.
  • Print the volume of the triangular pyramid.
  • Print the volume of the square pyramid.
  • Print the volume of the pentagonal pyramid.
  • Print the volume of the hexagonal pyramid.
  • The Exit of the Program.

Below is the implementation:

# Give the base as user input using the float(input()) function and store it in a variable.
gvn_baseval = float(input("Enter some random number = "))
# Give the height as user input using the float(input()) function and store it in another variable.
gvn_heigt = float(input("Enter some random number = "))
# Give the area as user input using the float(input()) function and store it in another variable.
gvn_area = float(input("Enter some random number = "))
# Calculate the volume of the triangular pyramid using the above given mathematical formula.
# Store it in another variable.
triangulr_pyramidvol = (0.1666) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the square pyramid using the above given mathematical formula.
# Store it in another variable.
squre_pyramidvol = (0.33) * gvn_baseval * gvn_baseval * gvn_heigt
# Calculate the volume of the pentagonal pyramid using the above given mathematical formula.
# Store it in another variable.
pentagonl_pyramidvol = (0.83) * gvn_area * gvn_baseval * gvn_heigt
# Calculate the volume of the hexagonal pyramid using the above given mathematical formula.
# Store it in another variable.
hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt
# Print the volume of the triangular pyramid.
print("The Volume of triangular base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", triangulr_pyramidvol)
# Print the volume of the square pyramid.
print("The Volume of square base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", squre_pyramidvol)
# Print the volume of the pentagonal pyramid.
print("The Volume of pentagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", pentagonl_pyramidvol)
# Print the volume of the hexagonal pyramid.
print("The Volume of hexagonal base pyramid with given base, height, area {",
      gvn_baseval, ",", gvn_heigt, ",", gvn_area, "} = ", hexgnl_pyramidvol)

Output:

Enter some random number = 4
Enter some random number = 6.2
Enter some random number = 15
The Volume of triangular base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 61.9752
The Volume of square base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 32.736000000000004
The Volume of pentagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 308.76
The Volume of hexagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 372.0