In the previous article, we have discussed Python Program to Calculate Volume of Ellipsoid
Tetrahedron:
A Tetrahedron is nothing more than a pyramid with a triangular base. It has four triangular faces, three on the sides or lateral faces, one on the bottom or base, and four vertices or corners. If all of the faces are congruent equilateral triangles, the tetrahedron is said to be regular.
Formula:
Area of Tetrahedron =sqrt(3)*side*side
Volume = (side ** 3 / (6 * math.sqrt(2)))
Given the side of a tetrahedron and the task is to calculate the area and volume of the given tetrahedron in python.
Examples:
Example1:
Input:
Given side = 5
Output:
The Area of Tetrahedron with the given side { 5 } = 43.301 The volume of Tetrahedron with the given side { 5 } = 14.731
- Python Program for Maximum Number of Squares that Can Fit in a Right Angle Isosceles Triangle
- Python Program to Calculate Area and Perimeter of Equilateral Triangle
- Python Program to Calculate the Area, Semi Perimeter and Perimeter of a Triangle
Example2:
Input:
Given side = 10
Output:
The Area of Tetrahedron with the given side { 10 } = 173.205 The volume of Tetrahedron with the given side { 10 } = 117.851
Program to Calculate Area and Volume of a Tetrahedron in Python
Below are the ways to calculate the area and volume of the given tetrahedron in python:
Method #1: Using Mathematical Formula (Static Input)
Approach:
- Import math module using the import keyword.
- Give the side as static input and store it in a variable.
- Create a function to say TetrahedronArea() which takes the given side as an argument and returns the area of the given tetrahedron.
- Inside the function, calculate the area of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
- Store it in a variable.
- Return the above result.
- Create another function to say Tetrahedronvolume() which takes the given side as an argument and returns the volume of the given tetrahedron.
- Inside the function, calculate the volume of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
- Store it in another variable.
- Return the above result.
- Pass the given side as an argument to the TetrahedronArea() function and round it off to up to 3 places after the decimal point using the round() function.
- Store it in another variable.
- Print the above result which is the area of the given tetrahedron.
- Pass the given side as an argument to the Tetrahedronvolume() function and round it off to up to 3 places after the decimal point using the round() function.
- Store it in another variable.
- Print the above result which is the volume of the given tetrahedron.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Create a function to say TetrahedronArea() which takes the given side as an argument # and returns the area of the given tetrahedron. def TetrahedronArea(gvn_side): # Inside the function, calculate the area of a given tetrahedron using the above # given mathematical formula and math.sqrt() function. # Store it in a variable. rslt_area = math.sqrt(3) * (gvn_side * gvn_side) # Return the above result. return rslt_area # Create another function to say Tetrahedronvolume() which takes the given side as an # argument and returns the volume of the given tetrahedron. def Tetrahedronvolume(gvn_side): # Inside the function, calculate the volume of a given tetrahedron using the above # given mathematical formula and math.sqrt() function. # Store it in a variable. rslt_vol = (gvn_side ** 3 / (6 * math.sqrt(2))) # Return the above result. return rslt_vol # Give the side as static input and store it in a variable. gvn_side = 10 # Pass the given side as an argument to the TetrahedronArea() function and round it # off to up to 3 places after the decimal point using the round() function. # Store it in another variable. area_tetrahdrn = round(TetrahedronArea(gvn_side), 3) # Print the above result which is the area of the given tetrahedron. print( "The Area of Tetrahedron with the given side {", gvn_side, "} = ", area_tetrahdrn) # Pass the given side as an argument to the Tetrahedronvolume() function and round it off # to up to 3 places after the decimal point using the round() function. # Store it in another variable. volum_tetrahdrn = round(Tetrahedronvolume(gvn_side), 3) # Print the above result which is the volume of the given tetrahedron. print( "The volume of Tetrahedron with the given side {", gvn_side, "} = ", volum_tetrahdrn)
#include <cmath> #include <iostream> using namespace std; int main() { int gvn_side = 10; double area_tetrahdrn = sqrt(3) * (gvn_side * gvn_side); double volum_tetrahdrn = (pow(gvn_side, 3) / (6 * sqrt(2))); cout << "The Area of Tetrahedron with the given side {" << gvn_side << "} = " << area_tetrahdrn << endl; cout << "The Volume of Tetrahedron with the given side {" << gvn_side << "} = " << volum_tetrahdrn << endl; return 0; }
Output:
The Area of Tetrahedron with the given side { 10 } = 173.205 The volume of Tetrahedron with the given side { 10 } = 117.851
Method #2: Using Mathematical Formula (User Input)
Approach:
- Import math module using the import keyword.
- Give the side as user input using the int(input()) function and store it in a variable.
- Create a function to say TetrahedronArea() which takes the given side as an argument and returns the area of the given tetrahedron.
- Inside the function, calculate the area of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
- Store it in a variable.
- Return the above result.
- Create another function to say Tetrahedronvolume() which takes the given side as an argument and returns the volume of the given tetrahedron.
- Inside the function, calculate the volume of a given tetrahedron using the above given mathematical formula and math.sqrt() function.
- Store it in another variable.
- Return the above result.
- Pass the given side as an argument to the TetrahedronArea() function and round it off to up to 3 places after the decimal point using the round() function.
- Store it in another variable.
- Print the above result which is the area of the given tetrahedron.
- Pass the given side as an argument to the Tetrahedronvolume() function and round it off to up to 3 places after the decimal point using the round() function.
- Store it in another variable.
- Print the above result which is the volume of the given tetrahedron.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Create a function to say TetrahedronArea() which takes the given side as an argument # and returns the area of the given tetrahedron. def TetrahedronArea(gvn_side): # Inside the function, calculate the area of a given tetrahedron using the above # given mathematical formula and math.sqrt() function. # Store it in a variable. rslt_area = math.sqrt(3) * (gvn_side * gvn_side) # Return the above result. return rslt_area # Create another function to say Tetrahedronvolume() which takes the given side as an # argument and returns the volume of the given tetrahedron. def Tetrahedronvolume(gvn_side): # Inside the function, calculate the volume of a given tetrahedron using the above # given mathematical formula and math.sqrt() function. # Store it in a variable. rslt_vol = (gvn_side ** 3 / (6 * math.sqrt(2))) # Return the above result. return rslt_vol # Give the side as user input using the int(input()) function and store it in a variable. gvn_side = int(input("Enter some random number = ")) # Pass the given side as an argument to the TetrahedronArea() function and round it # off to up to 3 places after the decimal point using the round() function. # Store it in another variable. area_tetrahdrn = round(TetrahedronArea(gvn_side), 3) # Print the above result which is the area of the given tetrahedron. print( "The Area of Tetrahedron with the given side {", gvn_side, "} = ", area_tetrahdrn) # Pass the given side as an argument to the Tetrahedronvolume() function and round it off # to up to 3 places after the decimal point using the round() function. # Store it in another variable. volum_tetrahdrn = round(Tetrahedronvolume(gvn_side), 3) # Print the above result which is the volume of the given tetrahedron. print( "The volume of Tetrahedron with the given side {", gvn_side, "} = ", volum_tetrahdrn)
Output:
Enter some random number = 5 The Area of Tetrahedron with the given side { 5 } = 43.301 The volume of Tetrahedron with the given side { 5 } = 14.731