Hello everyone, welcome to my new tutorial on Java Program to calculate Area and Volume of Cube.
In the previous tutorial, you have learned Java Program to Calculate Area of a Trapezium. In this tutorial, you will learn to find the Area and volume of a cube in Java. So let’s find it.
We can calculate the area and volume of a cube using the following formula.
Area of cube = 6a2
Where,
- a is the side length of cube
Volume of cube = Side3
Where,
- Side is the length of any edge of the cube.
Now let’s write the program.
Java Program to Calculate Area and Volume of Cube
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; public class Cube { public static void main(String[] args) { double s, area, volume; Scanner in = new Scanner(System.in); // Asking the user to enter the side of the cube System.out.println(" Enter the side of the cube :"); s = in.nextDouble(); area = 6 *(s*s); // Calculating area of cube volume = s * s * s; // Calculating volume of cube System.out.println(" Area of Cube : " + area +" square units"); System.out.println(" Volume of Cube : " + volume +" cubic unit); } } |
What we did ?
- First of all we have imported the util package.
- Then created a class and named it Cube.
- After that started main() method of the program.
- Then inside main() method declared variables.
- Then created object of the Scanner class.
- Now asked the user to enter the side of the cube. Then read the value of side of the cube entered by the user.
- Then calculated area of the cube using the formula area = 6 *(s*s)
- After that calculated the volume of the cube using the formula volume = s * s * s.
- Then finally displayed the value of the area and the volume of the cube on the screen.
Now let’s check the output of the above code.
Output:
1 2 3 4 |
Enter the side of the cube : 5 Area of Cube : 150.0 square units Volume of Cube : 125.0 cubic unit |
So in this tutorial, we have learned Java Program to calculate Area and Volume of Cube. In the next tutorial, you will learn Java Program to calculate Area and Volume of Cuboid.
Related Tutorials…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Right Triangle
- Java Program to calculate Area and Perimeter of Rectangle
- Java Program to calculate Area and Perimeter of Square
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Volume of Cone
- Java Program to calculate Surface Area and Volume of Sphere