Find the volume of hexagonal prism
A hexagonal prism is a three-dimensional geometric shape that has hexagonal bases and rectangular faces connecting the bases. Finding the volume of a hexagonal prism involves calculating the amount of space enclosed within its boundaries. This calculation is important in various fields such as architecture, engineering, and geometry, where hexagonal prisms are used to model buildings, containers, and structures.
Problem Statement
Given the length of the base side and the height of the hexagonal prism, the task is to calculate its volume. The formula for finding the volume of a hexagonal prism in terms of its base side length 'a' and height 'h' is:
Volume = (3√3 / 2) * a² * h
Example Scenario
Imagine you are an architect designing a storage container in the shape of a hexagonal prism. To determine the capacity of the container and plan for the amount of material it can hold, you need to calculate its volume. This calculation helps you optimize storage space and accurately estimate material requirements.
Idea to Solve the Problem
To solve this problem, we can follow these steps:
- Accept the base side length and the height of the hexagonal prism as inputs.
- Use the formula to calculate the volume of the hexagonal prism.
- Display the calculated volume.
Pseudocode
function hexagonal_volume(base, height):
volume = (3√3 / 2) * base * base * height
return volume
main:
base1 = 4
height1 = 8
base2 = 5
height2 = 5
base3 = 6
height3 = 5.8
volume1 = hexagonal_volume(base1, height1)
volume2 = hexagonal_volume(base2, height2)
volume3 = hexagonal_volume(base3, height3)
print("Hexagonal Prism Size [ base :", base1, "height :", height1, "]")
print("Volume :", volume1)
print("Hexagonal Prism Size [ base :", base2, "height :", height2, "]")
print("Volume :", volume2)
print("Hexagonal Prism Size [ base :", base3, "height :", height3, "]")
print("Volume :", volume3)
Algorithm Explanation
- Define a function
hexagonal_volume
that takes the base side length and height as inputs. - Inside the function, use the provided formula to calculate the volume of the hexagonal prism.
- In the
main
function, set three test cases with different values for the base side length and height. - Calculate the volumes for each test case by calling the
hexagonal_volume
function. - Display the calculated volumes along with their respective base side length and height values.
Code Solution
//C Program
//Find the volume of hexagonal prism
#include <stdio.h>
#include <math.h>
//Calculate volume of hexagonal prism by given base and height
void hexagonal_volume(double base, double height)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
double volume = 3 * (sqrt(3)) * base * base * height / 2;
//Display result
printf(" Hexagonal Prism Size [ base : %lf, height : %lf] ", base, height);
printf("\n Volume : %lf\n\n", volume);
}
int main()
{
//Test Case
hexagonal_volume(4, 8);
hexagonal_volume(5, 5);
hexagonal_volume(6, 5.8);
return 0;
}
Output
Hexagonal Prism Size [ base : 4.000000, height : 8.000000]
Volume : 332.553755
Hexagonal Prism Size [ base : 5.000000, height : 5.000000]
Volume : 324.759526
Hexagonal Prism Size [ base : 6.000000, height : 5.800000]
Volume : 542.478313
// Java Program
// Find the volume of hexagonal prism
class Hexagonal
{
//Calculate volume of hexagonal prism by given base and height
public void hexagonal_volume(double base, double height)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
double volume = 3 * (Math.sqrt(3)) * base * base * height / 2;
//Display result
System.out.print(" Hexagonal Prism Size [ base : " + base + ", height : " + height + "] ");
System.out.print("\n Volume : " + volume + "\n\n");
}
public static void main(String[] args)
{
Hexagonal obj = new Hexagonal();
//Test Cases
obj.hexagonal_volume(4, 8);
obj.hexagonal_volume(5, 5);
obj.hexagonal_volume(6, 5.8);
}
}
Output
Hexagonal Prism Size [ base : 4.0, height : 8.0]
Volume : 332.55375505322445
Hexagonal Prism Size [ base : 5.0, height : 5.0]
Volume : 324.7595264191645
Hexagonal Prism Size [ base : 6.0, height : 5.8]
Volume : 542.4783129305723
// C++ Program
// Find the volume of hexagonal prism
#include<iostream>
#include <math.h>
using namespace std;
class Hexagonal
{
public:
//Calculate volume of hexagonal prism by given base and height
void hexagonal_volume(double base, double height)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
double volume = 3 *(sqrt(3)) *base *base *height / 2;
cout << " Hexagonal Prism Size [ base : " << base << ", height : " << height << "] ";
cout << "\n Volume : " << volume << "\n\n";
}
};
int main()
{
Hexagonal obj = Hexagonal();
//Test Cases
obj.hexagonal_volume(4, 8);
obj.hexagonal_volume(5, 5);
obj.hexagonal_volume(6, 5.8);
return 0;
}
Output
Hexagonal Prism Size [ base : 4, height : 8]
Volume : 332.554
Hexagonal Prism Size [ base : 5, height : 5]
Volume : 324.76
Hexagonal Prism Size [ base : 6, height : 5.8]
Volume : 542.478
// C# Program
// Find the volume of hexagonal prism
using System;
class Hexagonal
{
//Calculate volume of hexagonal prism by given base and height
public void hexagonal_volume(double base_side, double height)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
double volume = 3 * (Math.Sqrt(3)) * base_side * base_side * height / 2;
Console.Write(" Hexagonal Prism Size [ base : " + base_side + ", height : " + height + "] ");
Console.Write("\n Volume : " + volume + "\n\n");
}
public static void Main(String[] args)
{
Hexagonal obj = new Hexagonal();
//Test Cases
obj.hexagonal_volume(4, 8);
obj.hexagonal_volume(5, 5);
obj.hexagonal_volume(6, 5.8);
}
}
Output
Hexagonal Prism Size [ base : 4, height : 8]
Volume : 332.553755053224
Hexagonal Prism Size [ base : 5, height : 5]
Volume : 324.759526419165
Hexagonal Prism Size [ base : 6, height : 5.8]
Volume : 542.478312930572
<?php
// Php Program
// Find the volume of hexagonal prism
class Hexagonal
{
//Calculate volume of hexagonal prism by given base and height
public function hexagonal_volume($base, $height)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
$volume = 3 *(sqrt(3)) *$base *$base *$height / 2;
//Display result
echo(" Hexagonal Prism Size [ base : ". $base .", height : ". $height ."] ");
echo("\n Volume : ". $volume ."\n\n");
}
}
function main()
{
$obj = new Hexagonal();
//Test Cases
$obj->hexagonal_volume(4, 8);
$obj->hexagonal_volume(5, 5);
$obj->hexagonal_volume(6, 5.8);
}
main();
Output
Hexagonal Prism Size [ base : 4, height : 8]
Volume : 332.55375505322
Hexagonal Prism Size [ base : 5, height : 5]
Volume : 324.75952641916
Hexagonal Prism Size [ base : 6, height : 5.8]
Volume : 542.47831293057
// Node Js Program
// Find the volume of hexagonal prism
class Hexagonal
{
//Calculate volume of hexagonal prism by given base and height
hexagonal_volume(base, height)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
var volume = 3 *(Math.sqrt(3)) *base *base *height / 2;
//Display result
process.stdout.write(" Hexagonal Prism Size [ base : " + base + ", height : " + height + "] ");
process.stdout.write("\n Volume : " + volume + "\n\n");
}
}
function main(args)
{
var obj = new Hexagonal();
//Test Cases
obj.hexagonal_volume(4, 8);
obj.hexagonal_volume(5, 5);
obj.hexagonal_volume(6, 5.8);
}
main();
Output
Hexagonal Prism Size [ base : 4, height : 8]
Volume : 332.55375505322445
Hexagonal Prism Size [ base : 5, height : 5]
Volume : 324.7595264191645
Hexagonal Prism Size [ base : 6, height : 5.8]
Volume : 542.4783129305723
# Python 3 Program
# Find the volume of hexagonal prism
import math
class Hexagonal :
# Calculate volume of hexagonal prism by given base and height
def hexagonal_volume(self, base, height) :
# Formula
# Here a is base and h is height
# (3√3)
# ----- a²h
# 2
volume = 3 * (math.sqrt(3)) * base * base * height / 2
# Display result
print(" Hexagonal Prism Size [ base : ", base ,", height : ", height ,"] ", end = "")
print("\n Volume : ", volume ,"\n\n", end = "")
def main() :
obj = Hexagonal()
# Test Cases
obj.hexagonal_volume(4, 8)
obj.hexagonal_volume(5, 5)
obj.hexagonal_volume(6, 5.8)
if __name__ == "__main__": main()
Output
Hexagonal Prism Size [ base : 4 , height : 8 ]
Volume : 332.55375505322445
Hexagonal Prism Size [ base : 5 , height : 5 ]
Volume : 324.7595264191645
Hexagonal Prism Size [ base : 6 , height : 5.8 ]
Volume : 542.4783129305723
# Ruby Program
# Find the volume of hexagonal prism
class Hexagonal
# Calculate volume of hexagonal prism by given base and height
def hexagonal_volume(base, height)
# Formula
# Here a is base and h is height
# (3√3)
# ----- a²h
# 2
volume = 3 * (Math.sqrt(3)) * base * base * height / 2
# Display result
print(" Hexagonal Prism Size [ base : ", base ,", height : ", height ,"] ")
print("\n Volume : ", volume ,"\n\n")
end
end
def main()
obj = Hexagonal.new()
# Test Cases
obj.hexagonal_volume(4, 8)
obj.hexagonal_volume(5, 5)
obj.hexagonal_volume(6, 5.8)
end
main()
Output
Hexagonal Prism Size [ base : 4, height : 8]
Volume : 332.55375505322445
Hexagonal Prism Size [ base : 5, height : 5]
Volume : 324.7595264191645
Hexagonal Prism Size [ base : 6, height : 5.8]
Volume : 542.4783129305723
// Scala Program
// Find the volume of hexagonal prism
class Hexagonal
{
//Calculate volume of hexagonal prism by given base and height
def hexagonal_volume(base: Double, height: Double): Unit = {
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
var volume: Double = 3 * (Math.sqrt(3)) * base * base * height / 2;
//Display result
print(" Hexagonal Prism Size [ base : " + base + ", height : " + height + " ] ");
print("\n Volume : " + volume + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Hexagonal = new Hexagonal();
//Test Cases
obj.hexagonal_volume(4, 8);
obj.hexagonal_volume(5, 5);
obj.hexagonal_volume(6, 5.8);
}
}
Output
Hexagonal Prism Size [ base : 4.0, height : 8.0 ]
Volume : 332.55375505322445
Hexagonal Prism Size [ base : 5.0, height : 5.0 ]
Volume : 324.7595264191645
Hexagonal Prism Size [ base : 6.0, height : 5.8 ]
Volume : 542.4783129305723
// Swift Program
// Find the volume of hexagonal prism
import Foundation
class Hexagonal
{
//Calculate volume of hexagonal prism by given base and height
func hexagonal_volume(_ base: Double, _ height: Double)
{
// Formula
// Here a is base and h is height
// (3√3)
// ----- a²h
// 2
let volume: Double = 3 * (sqrt(3)) * base * base * height / 2;
//Display result
print(" Hexagonal Prism Size [ base : ", base ,", height : ", height ,"] ", terminator: "");
print("\n Volume : ", volume ,"\n\n", terminator: "");
}
}
func main()
{
let obj: Hexagonal = Hexagonal();
//Test Cases
obj.hexagonal_volume(4, 8);
obj.hexagonal_volume(5, 5);
obj.hexagonal_volume(6, 5.8);
}
main();
Output
Hexagonal Prism Size [ base : 4.0 , height : 8.0 ]
Volume : 332.553755053224
Hexagonal Prism Size [ base : 5.0 , height : 5.0 ]
Volume : 324.759526419165
Hexagonal Prism Size [ base : 6.0 , height : 5.8 ]
Volume : 542.478312930572
Output Explanation
The code calculates the volume for each test case and displays the results. For a hexagonal prism with a base side length of 4 and a height of 8, the volume is approximately 332.553755. Similarly, for a base side length of 5 and height of 5, the volume is approximately 324.759526. The third test case follows the same pattern with a different set of values.
Time Complexity
The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations and the value of the cube root of 3, which are calculated in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment