Find the volume of cube

A cube is a three-dimensional geometric shape that has six equal square faces and all angles of 90 degrees. Finding the volume of a cube involves calculating the amount of space enclosed within its boundaries. This calculation is fundamental in various fields, including mathematics, engineering, and manufacturing, where cubes are used to represent regular objects.

Problem Statement

Given the length of an edge of the cube, the task is to calculate its volume. The formula for finding the volume of a cube in terms of its edge length 'e' is:

Volume = e^3

Example Scenario

Imagine you are an architect designing a cubic storage container. To determine the maximum amount of material or capacity the container can hold, you need to calculate its volume. This calculation helps you optimize space utilization and plan storage requirements accurately.

Idea to Solve the Problem

To solve this problem, we can follow these steps:

1. Accept the length of an edge of the cube as input.
2. Use the formula to calculate the volume of the cube.
3. Display the calculated volume.

Pseudocode

``````function cube_volume(edge):
volume = edge * edge * edge
return volume

main:
edge1 = 7.2
edge2 = 9
edge3 = 4
edge4 = 4.7

volume1 = cube_volume(edge1)
volume2 = cube_volume(edge2)
volume3 = cube_volume(edge3)
volume4 = cube_volume(edge4)

print("Cube Size [ edge :", edge1, "]")
print("Volume :", volume1)

print("Cube Size [ edge :", edge2, "]")
print("Volume :", volume2)

print("Cube Size [ edge :", edge3, "]")
print("Volume :", volume3)

print("Cube Size [ edge :", edge4, "]")
print("Volume :", volume4)``````

Algorithm Explanation

1. Define a function `cube_volume` that takes the edge length as input.
2. Inside the function, use the provided formula to calculate the volume of the cube.
3. In the `main` function, set four test cases with different values for the edge length.
4. Calculate the volumes for each test case by calling the `cube_volume` function.
5. Display the calculated volumes along with their respective edge lengths.

Code Solution

``````//C Program
//Find the volume of cube
#include <stdio.h>

//Calculate volume of cube by given edge
void cube_volume(double edge)
{
// Formula
// a³  here a is edge
// edge * edge * edge
double volume = edge * edge * edge;
//Display result
printf(" Cube Size [ edge : %lf] ", edge);
printf("\n Volume : %lf\n\n", volume);
}
int main()
{
//Test Case
cube_volume(7.2);
cube_volume(9);
cube_volume(4);
cube_volume(4.7);
return 0;
}``````

Output

`````` Cube Size [ edge : 7.200000]
Volume : 373.248000

Cube Size [ edge : 9.000000]
Volume : 729.000000

Cube Size [ edge : 4.000000]
Volume : 64.000000

Cube Size [ edge : 4.700000]
Volume : 103.823000
``````
``````// Java Program
// Find the volume of cube
class Cube
{
//Calculate volume of cube by given edge
public void cube_volume(double edge)
{
// Formula
// a³  here a is edge
// edge * edge * edge
double volume = edge * edge * edge;
//Display result
System.out.print(" Cube Size [ edge : " + edge + "] ");
System.out.print("\n Volume  : " + volume + "\n\n");
}
public static void main(String[] args)
{
Cube obj = new Cube();
//Test Case
obj.cube_volume(7.2);
obj.cube_volume(9);
obj.cube_volume(4);
obj.cube_volume(4.7);
}
}``````

Output

`````` Cube Size [ edge : 7.2]
Volume  : 373.24800000000005

Cube Size [ edge : 9.0]
Volume  : 729.0

Cube Size [ edge : 4.0]
Volume  : 64.0

Cube Size [ edge : 4.7]
Volume  : 103.82300000000002
``````
``````// C++ Program
// Find the volume of cube
#include<iostream>

using namespace std;
class Cube
{
public:
//Calculate volume of cube by given edge
void cube_volume(double edge)
{
// Formula
// a³  here a is edge
// edge *edge *edge
double volume = edge *edge *edge;
cout << " Cube Size [ edge : " << edge << "] ";
cout << "\n Volume : " << volume << "\n\n";
}
};
int main()
{
Cube obj = Cube();
//Test Case
obj.cube_volume(7.2);
obj.cube_volume(9);
obj.cube_volume(4);
obj.cube_volume(4.7);
return 0;
}``````

Output

`````` Cube Size [ edge : 7.2]
Volume : 373.248

Cube Size [ edge : 9]
Volume : 729

Cube Size [ edge : 4]
Volume : 64

Cube Size [ edge : 4.7]
Volume : 103.823
``````
``````// C# Program
// Find the volume of cube
using System;
class Cube
{
//Calculate volume of cube by given edge
public void cube_volume(double edge)
{
// Formula
// a³  here a is edge
// edge * edge * edge
double volume = edge * edge * edge;
Console.Write(" Cube Size [ edge : " + edge + "] ");
Console.Write("\n Volume : " + volume + "\n\n");
}
public static void Main(String[] args)
{
Cube obj = new Cube();
//Test Case
obj.cube_volume(7.2);
obj.cube_volume(9);
obj.cube_volume(4);
obj.cube_volume(4.7);
}
}``````

Output

`````` Cube Size [ edge : 7.2]
Volume : 373.248

Cube Size [ edge : 9]
Volume : 729

Cube Size [ edge : 4]
Volume : 64

Cube Size [ edge : 4.7]
Volume : 103.823
``````
``````<?php
// Php Program
// Find the volume of cube
class Cube
{
//Calculate volume of cube by given edge
public 	function cube_volume(\$edge)
{
// Formula
// a³  here a is edge
// edge *edge *edge
\$volume = \$edge *\$edge *\$edge;
//Display result
echo(" Cube Size [ edge : ". \$edge ."] ");
echo("\n Volume : ". \$volume ."\n\n");
}
}

function main()
{
\$obj = new Cube();
//Test Case
\$obj->cube_volume(7.2);
\$obj->cube_volume(9);
\$obj->cube_volume(4);
\$obj->cube_volume(4.7);
}
main();``````

Output

`````` Cube Size [ edge : 7.2]
Volume : 373.248

Cube Size [ edge : 9]
Volume : 729

Cube Size [ edge : 4]
Volume : 64

Cube Size [ edge : 4.7]
Volume : 103.823
``````
``````// Node Js Program
// Find the volume of cube
class Cube
{
//Calculate volume of cube by given edge
cube_volume(edge)
{
// Formula
// a³  here a is edge
// edge *edge *edge
var volume = edge *edge *edge;
//Display result
process.stdout.write(" Cube Size [ edge : " + edge + "] ");
process.stdout.write("\n Volume : " + volume + "\n\n");
}
}

function main(args)
{
var obj = new Cube();
//Test Case
obj.cube_volume(7.2);
obj.cube_volume(9);
obj.cube_volume(4);
obj.cube_volume(4.7);
}
main();``````

Output

`````` Cube Size [ edge : 7.2]
Volume : 373.24800000000005

Cube Size [ edge : 9]
Volume : 729

Cube Size [ edge : 4]
Volume : 64

Cube Size [ edge : 4.7]
Volume : 103.82300000000002
``````
``````#  Python 3 Program
#  Find the volume of cube
class Cube :
# Calculate volume of cube by given edge
def cube_volume(self, edge) :
#  Formula
#  a³  here a is edge
#  edge * edge * edge
volume = edge * edge * edge
# Display result
print(" Cube Size [ edge : ", edge ,"] ")
print(" Volume : ", volume ,"\n")

def main() :
obj = Cube()
# Test Case
obj.cube_volume(7.2)
obj.cube_volume(9)
obj.cube_volume(4)
obj.cube_volume(4.7)

if __name__ == "__main__": main()``````

Output

`````` Cube Size [ edge :  7.2 ]
Volume :  373.24800000000005

Cube Size [ edge :  9 ]
Volume :  729

Cube Size [ edge :  4 ]
Volume :  64

Cube Size [ edge :  4.7 ]
Volume :  103.82300000000002
``````
``````#  Ruby Program
#  Find the volume of cube
class Cube

# Calculate volume of cube by given edge
def cube_volume(edge)

#  Formula
#  a³  here a is edge
#  edge * edge * edge
volume = edge * edge * edge
# Display result
print(" Cube Size [ edge  : ", edge ,"] ")
print("\n Volume  : ", volume ,"\n\n")
end
end
def main()

obj = Cube.new()
# Test Case
obj.cube_volume(7.2)
obj.cube_volume(9)
obj.cube_volume(4)
obj.cube_volume(4.7)
end
main()``````

Output

`````` Cube Size [ edge  : 7.2]
Volume  : 373.24800000000005

Cube Size [ edge  : 9]
Volume  : 729

Cube Size [ edge  : 4]
Volume  : 64

Cube Size [ edge  : 4.7]
Volume  : 103.82300000000002

``````
``````// Scala Program
// Find the volume of cube
class Cube
{
//Calculate volume of cube by given edge
def cube_volume(edge: Double): Unit = {
// Formula
// a³  here a is edge
// edge * edge * edge
var volume: Double = edge * edge * edge;
//Display result
print(" Cube Size [ edge : " + edge + "] ");
print("\n Volume : " + volume + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Cube = new Cube();
//Test Case
obj.cube_volume(7.2);
obj.cube_volume(9);
obj.cube_volume(4);
obj.cube_volume(4.7);
}
}``````

Output

`````` Cube Size [ edge : 7.2]
Volume : 373.24800000000005

Cube Size [ edge : 9.0]
Volume : 729.0

Cube Size [ edge : 4.0]
Volume : 64.0

Cube Size [ edge : 4.7]
Volume : 103.82300000000002
``````
``````// Swift Program
// Find the volume of cube
class Cube
{
//Calculate volume of cube by given edge
func cube_volume(_ edge: Double)
{
// Formula
// a³  here a is edge
// edge * edge * edge
let volume: Double = edge * edge * edge;
//Display result
print(" Cube Size [ edge : ", edge ,"] ");
print(" Volume : ", volume ,"\n");
}
}
func main()
{
let obj: Cube = Cube();
//Test Case
obj.cube_volume(7.2);
obj.cube_volume(9);
obj.cube_volume(4);
obj.cube_volume(4.7);
}
main();``````

Output

`````` Cube Size [ edge :  7.2 ]
Volume :  373.248

Cube Size [ edge :  9.0 ]
Volume :  729.0

Cube Size [ edge :  4.0 ]
Volume :  64.0

Cube Size [ edge :  4.7 ]
Volume :  103.823
``````

Output Explanation

The code calculates the volume for each test case and displays the results. For a cube with an edge length of 7.2, the volume is 373.248. Similarly, for edge lengths of 9, 4, and 4.7, the volumes are 729, 64, and 103.823, respectively.

Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations, which take a constant amount of time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

Comment

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.