In the previous article, we have seen Java Program to Find Volume and Surface Area of Frustum of Cone
In this article we are going to see how to find the volume of octahedron using Java programming language.
Java Program to Find Volume of Octahedron
Before Jumping into the program directly let’s see how we can find the volume of octahedron.
Explanation:
An regular octahedron is a polyhedron which has 8 faces 12 edges and 6 vertices and all are in the shape of equilateral triangles. Formula to find total volume of octahedron = sqrt(2)/3 * (side*side*side)
Example:
Let one side of octahedron be “l” = 5 So, volume of octahedron = sqrt(2)/3 * (side*side*side) => sqrt(2)/3 * (5*5*5) => sqrt(2)/3 * 125 = 58.9255
Let’s see different ways to find volume of octahedron.
Method-1: Java Program to Find Volume of Octahedron By Using Static Value
Approach:
- Declare an double variable say ‘
l
’ and assign the value to it, which holds the value of one side of the octahedron. - Find the volume of octahedron using the formula √2/3 * (side*side*side)
- Print the result.
Program:
import java.io.*; class Main { public static void main(String [] args) { // value of one side of the octahedron int l = 5; // formula to find vol of octahedron double vol = ((Math.sqrt(2))*(5*5*5))/3; System.out.println("The volume of octahedron is:" + vol); } }
Output: The volume of octahedron is:58.92556509887896
Method-2: Java Program to Find Volume of Octahedron By Using User Input Value
Approach:
- Declare an double variable say ‘
l
’ which holds the value of one side of the octahedron. - Take the value of
l
as user input using Scanner class. - Find the volume of octahedron using the formula √2/3 * (side*side*side)
- Print the result.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object Scanner s = new Scanner(System.in); System.out.println("Enter the value of side of the octahedron:"); int l = s.nextInt(); // formula to find vol of octahedron double vol = ((Math.sqrt(2))*(5*5*5))/3; System.out.println("The volume of octahedron is:" + vol); } }
Output: Enter the value of side of the octahedron: 4 The volume of octahedron is:58.92556509887896
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Articles: