This is the Java Program to Find the Surface Area and Volume of a Cuboid.
Given the dimensions of a cuboid, find out its surface area and volume.
The surface area and volume of a cuboid can be calculated using the formulas:
Surface Area = 2*(length*breadth + breadth*height + height*length).
Volume = length * breadth * height.
Here is the source code of the Java Program to Find the Surface Area and Volume of a Cuboid. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Find the Surface Area and Volume of a Cuboid
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Cuboid {
// Function to calculate the Surface Area and Volume of a Cuboid
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double length,breadth,height;
System.out.println("Enter the length, breadth and height of the cuboid");
try{
length=Double.parseDouble(br.readLine());
breadth=Double.parseDouble(br.readLine());
height=Double.parseDouble(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
if(length<=0 || breadth<=0 || height<=0){
System.out.println("Wrong Input");
return;
}
System.out.println("Volume = " + length*breadth*height);
System.out.println("Surface area = " +
2*(length*breadth + breadth*height + height*length));
}
}
1. In function main(), first, the length, breadth and height of the cuboid are taken as input.
2. The condition if(length<=0 || height<=0 || breadth<=0) checks if the input is valid or not.
3. The print statement displays the surface area and volume of the cuboid.
Time Complexity: O(1)
Case 1 (Simple Test Case): Enter the length, breadth and height of the cuboid 12 9.76 5.68 Volume = 665.2416 Surface area = 481.4336
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Practice BCA MCQs
- Practice Programming MCQs
- Check Java Books