Java Program to Find the Surface Area and Volume of a Cuboid
In this tutorial, we will learn how to find the surface area and volume of the cuboid in java. A cuboid is a 3D figure which consists of six faces, eight vertices, and twelve edges. The surface area of a cuboid is equal to the sum of the areas of its six rectangular faces. The volume of the cuboid is equal to the product of the area of one surface and height. But before moving further, if you are not familiar with the concept of data types in java, then do check the article on the topic Data Types in Java
Input: Enter the length of the cuboid: 5
Enter the breadth of the cuboid: 7
Enter the height of the cuboid: 2
Output: The surface area of the cuboid is: 118
The volume of the cuboid is: 70
Program 1: Find the Surface area and Volume of Cuboid
In this example, we will see how to calculate the surface area and volume of a cuboid in java.
Algorithm:
- Start
- Create an instance of the Scanner Class
- Declare variables to store the length, breadth, and height of the cuboid.
- Ask the user to initialize these variables.
- Declare variables to store the surface area and volume of the cuboid.
- Use the formula to calculate the values.
- Display the surface area and volume.
- Stop.
The below example demonstrates how to find the surface area and volume of a cuboid.
//Java Program to Find the Surface Area and Volume of Cuboid
import java.util.*;
public class Main
{
// Driver Code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the cuboid: ");
int length=sc.nextInt();
System.out.println("Enter the breadth of the cuboid: ");
int breadth=sc.nextInt();
System.out.println("Enter the height of the cuboid: ");
int height=sc.nextInt();
int surface_area=2*((length*breadth)+(breadth*height)+(height*length));
System.out.println("The surface area of the cuboid is: "+surface_area);
int volume=length*breadth*height;
System.out.println("The volume of the cuboid is: "+volume);
}
}
Enter the length of the cuboid: 4
Enter the breadth of the cuboid: 9
Enter the height of the cuboid: 6
The surface area of the cuboid is: 228
The volume of the cuboid is: 216
Program 2: Find the Surface area and Volume of Cuboid
In this example, we will see how to calculate the surface area and volume of a cuboid in java.
Algorithm:
- Start
- Create an instance of the Scanner Class
- Declare variables to store the length, breadth, and height of the cuboid.
- Ask the user to initialize these variables.
- Call two user-defined methods to calculate the surface area and the volume of the cuboid.
- Pass the entered values as arguments.
- Declare variables to store the surface area and volume of the cuboid.
- Use the formula to calculate the values.
- Return the calculated values.
- Display the calculated surface area and volume of the cuboid.
- Stop.
The below example demonstrates how to find the surface area and volume of a cuboid.
//Java Program to Find the Surface Area and Volume of Cuboid
import java.util.*;
public class Main
{
// Driver Code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the cuboid: ");
int length=sc.nextInt();
System.out.println("Enter the breadth of the cuboid: ");
int breadth=sc.nextInt();
System.out.println("Enter the height of the cuboid: ");
int height=sc.nextInt();
//Calculate the surface area
int area= findSurfaceArea(length,breadth,height);
System.out.println("The surface area of the cuboid is: "+area);
//Calculate the volume
int vol=findVolume(length,breadth,height);
System.out.println("The volume of the cuboid is: "+vol);
}
//User-defined methood for surface area of cuboid
public static int findSurfaceArea(int length,int breadth, int height)
{
int surface_area=2*((length*breadth)+(breadth*height)+(height*length));
return surface_area;
}
//User-defined methood for volume of cuboid
public static int findVolume(int length,int breadth, int height)
{
int volume=length*breadth*height;
return volume;
}
}
Enter the length of the cuboid: 9
Enter the breadth of the cuboid: 3
Enter the height of the cuboid: 7
The surface area of the cuboid is: 222
The volume of the cuboid is: 189