Program for volume of Pyramid

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

There are many types of pyramids. Most often, they are named after the type of base they have. Let’s look at some common types of pyramids below.

Volume of a square pyramid [base of Pyramid is square] = (1/3) * (b^2) * h
Volume of a triangular pyramid [base of Pyramid is triangle] = (1/6) * a * b * h
Volume of a pentagonal pyramid [base of Pyramid is pentagonal] = (5/6) * a * b * h
Volume of a hexagonal pyramid [base of Pyramid is hexagonal] = a * b * h

Below is code for calculating volume of Pyramids :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the volume.
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the volume.
  
// Function to find the volume 
// of triangular pyramid
function volumeTriangular($a, $b, $h)
{
    $vol = (0.1666) * $a * $b * $h;
    return $vol;
}
  
// Function to find the
// volume of square pyramid
function volumeSquare($b, $h)
{
    $vol = (0.33) * $b * $b * $h;
    return $vol;
}
  
// Function to find the volume 
// of pentagonal pyramid
function volumePentagonal($a, $b, $h)
{
    $vol = (0.83) * $a * $b * $h;
    return $vol;
}
  
// Function to find the volume 
// of hexagonal pyramid
function volumeHexagonal($a, $b, $h)
{
    $vol = $a * $b * $h;
    return $vol;
}
  
// Driver Code
$b = 4; $h = 9; $a = 4;
echo ("Volume of triangular base pyramid is ");
echo( volumeTriangular($a, $b, $h));
echo("\n");
echo ("Volume of square base pyramid is ");
echo( volumeSquare($b, $h));
echo("\n");
echo ("Volume of pentagonal base pyramid is ");
echo(volumePentagonal($a, $b, $h));
echo("\n");
echo("Volume of Hexagonal base pyramid is ");
echo(volumeHexagonal($a, $b, $h));
  
// This code is contributed by vt_m 
?>

chevron_right



Output :

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


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m