Wednesday, January 8, 2025
Google search engine
HomeData Modelling & AIVolume of cube using its space diagonal

Volume of cube using its space diagonal

Given the length of space diagonal of a cube as d. The task is to calculate the volume occupied by the cube with the given length of space diagonal. Space diagonal is a line connecting two vertices that are not on the same face.
 

Cube

Examples: 
 

Input: d = 5 
Output: Volume of Cube: 24.0563

Input: d = 10
Output: Volume of Cube: 192.45

 

 

Volume of cube whose space diagonal is given: \sqrt{3} \frac{d^3}{9}

Proof:
 

Let d = the length of diagonal |AB| and 
let a = the length of each side of the cube. 
Pythagoras #1 in triangle ACD: 
(AC)^2=(a)^2+(a)^2 AC = \sqrt{2}a $
Pythagoras #2 in triangle ABC: 
(AB)^2=(\sqrt{2}a)^2+(a)^2 AB = \sqrt{3}a $
Now we can solve for a in terms of d: 
$$a=\frac{d}{ \sqrt{3} }$
This means that the volume V is: 
$V=a^3=\frac{d^3}{3 \sqrt{3}} = \frac{\sqrt{3}d^3}{9}$$

Below is the required implementation: 
 

C++




// C++ program to find the volume occupied
// by Cube with given space diagonal
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate Volume
float CubeVolume(float d)
{
    float Volume;
 
    // Formula to find Volume
    Volume = (sqrt(3) * pow(d, 3)) / 9;
 
    return Volume;
}
 
// Drivers code
int main()
{
 
    // space diagonal of Cube
    float d = 5;
 
    cout << "Volume of Cube: "
         << CubeVolume(d);
 
    return 0;
}


Java




// Java program to find the volume occupied
// by Cube with given space diagonal
 
public class GFG{
     
    // Function to calculate Volume
    static float CubeVolume(float d)
    {
        float Volume;
     
        // Formula to find Volume
        Volume = (float) (Math.sqrt(3) * Math.pow(d, 3)) / 9;
     
        return Volume;
    }
     
    // Drivers code
    public static void main(String []args)
    {
     
        // space diagonal of Cube
        float d = 5;
     
        System.out.println("Volume of Cube: " + CubeVolume(d));
     
    }
 
    // This code is contributed by Ryuga
    }


Python3




# Python 3 program to find the volume occupied
# by Cube with given space diagonal
from math import sqrt, pow
 
# Function to calculate Volume
def CubeVolume(d):
 
    # Formula to find Volume
    Volume = (sqrt(3) * pow(d, 3)) / 9
 
    return Volume
 
# Drivers code
if __name__ == '__main__':
 
    # space diagonal of Cube
    d = 5
 
    print("Volume of Cube:",'{0:.6}' .
                format(CubeVolume(d)))
 
# This code is contributed
# by SURENDRA_GANGWAR


C#




// C# program to find the volume occupied
// by Cube with given space diagonal
using System;
 
public class GFG{
     
    // Function to calculate Volume
    static float CubeVolume(float d)
    {
        float Volume;
     
        // Formula to find Volume
        Volume = (float) (Math.Sqrt(3) * Math.Pow(d, 3)) / 9;
     
        return Volume;
    }
     
    // Drivers code
    public static void Main()
    {
     
        // space diagonal of Cube
        float d = 5;
     
        Console.WriteLine("Volume of Cube: {0:F4}" , CubeVolume(d));
     
    }
 
    // This code is contributed by mits
    }


PHP




<?php
// PHP program to find the volume occupied
// by Cube with given space diagonal
 
// Function to calculate Volume
function CubeVolume($d)
{
    $Volume;
 
    // Formula to find Volume
    $Volume = (sqrt(3) * pow($d, 3)) / 9;
 
    return $Volume;
}
 
// Driver code
 
// space diagonal of Cube
$d = 5;
 
echo "Volume of Cube: ",
         CubeVolume($d);
     
// This code is contributed by akt_mit
?>


Javascript




<script>
 
// javascript program to find the volume occupied
// by Cube with given space diagonal
 
// Function to calculate Volume
function CubeVolume( d)
{
    let Volume;
 
    // Formula to find Volume
    Volume = (Math.sqrt(3) * Math.pow(d, 3)) / 9;
 
    return Volume;
}
 
// Drivers code
 
    // space diagonal of Cube
    let d = 5;
 
   document.write( "Volume of Cube: "
         + CubeVolume(d).toFixed(4));
          
// This code contributed by gauravrajput1
 
</script>


Output: 

Volume of Cube: 24.0563

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments