Write Java Program to find Volume and Surface Area of a Cylinder with example. Before we step into the Java Program to find Volume and Surface Area of a Cylinder, Let see the definitions and formulas behind Surface Area of a Cylinder, Top or Bottom Surface Area, Lateral Surface Area and Volume of a Cylinder.

## Surface Area of a Cylinder

If we know the radius and height of the cylinder then we can calculate the surface area of a cylinder using the formula:

Surface Area of a Cylinder = 2πr² + 2πrh (Where r is a radius and h is the height of the cylinder).

**Volume of a Cylinder**

The amount of space inside the Cylinder is called as Volume. If we know the height of a cylinder then we can calculate the Volume of a cylinder using formula:

- Volume of a Cylinder = πr²h
- The Lateral Surface Area of a Cylinder = 2πrh
- We can calculate the Top Or Bottom Surface Area of a Cylinder = πr²

## Java Program to find Volume and Surface Area of a Cylinder

This Java program allows the user to enter the value of a radius and height. Using these values, we will calculate the Volume of a Cylinder, Lateral Surface Area of a Cylinder, Surface Area of a Cylinder and Top Or Bottom Surface Area of a Cylinder as per the formula.

// Java Program to find Volume and Surface Area of a Cylinder package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCylinder { private static Scanner sc; public static void main(String[] args) { // L = Lateral Surface Area of a Cylinder, T = Top Surface Area double radius, sa, Volume, height, L, T; sc = new Scanner(System.in); System.out.println("\n Please Enter the radius of a Cylinder : "); radius = sc.nextDouble(); System.out.println("\n Please Enter the Height of a Cylinder : "); height = sc.nextDouble(); sa = 2 * Math.PI * radius * (radius + height); Volume = Math.PI * radius * radius * height; L = 2 * Math.PI * radius * height; T = Math.PI * radius * radius; System.out.format("\n The Surface area of a Cylinder = %.2f", sa); System.out.format("\n The Volume of a Cylinder = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L); System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T); } }

**OUTPUT**

**ANALYSIS**

Following statements will allow the User to enter the radius and height of a cylinder and then we are assigning the user entered values to already declared variables called radius and height.

System.out.println("\n Please Enter the radius of a Cylinder : "); radius = sc.nextDouble(); System.out.println("\n Please Enter the Height of a Cylinder : "); height = sc.nextDouble();

Next, we are using the Mathematical Formula to calculate the surface area of a Cylinder

sa = 2 * Math.PI * radius * (radius + height);

In the next line, We are calculating the volume of of a Cylinder

Volume = Math.PI * radius * radius * height;

In the next line, We are calculating the Lateral Surface Area of a Cylinder and Top Or Bottom Surface Area of a Cylinder

L = 2 * Math.PI * radius * height; T = Math.PI * radius * radius;

Following System.out.format statements will help us to print the the Volume of a Cylinder, Lateral Surface Area of a Cylinder, Surface Area of a Cylinder and Top Or Bottom Surface Area of a Cylinder

System.out.format("\n The Surface area of a Cylinder = %.2f", sa); System.out.format("\n The Volume of a Cylinder = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L); System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);

From the above screenshot you can observe that, We have entered the Radius of a Cylinder = 4 and height = 6

**The Surface Area of a Cylinder** is

Surface Area of a Cylinder= 2πr² + 2πrh

It can also be written as

Surface Area of a Cylinder = 2πr (r+h)

Surface Area of a Cylinder = 2 * Math.PI * radius * (radius + height)

Surface Area of a Cylinder = 2 * 3.14 * 4 * (4 + 6);

Surface Area of a Cylinder = 251.2

**The Volume of a Cylinder** is

Volume of a Cylinder = πr²h

Volume of a Cylinder = Math.PI * radius * radius * height

Volume of a Cylinder = 3.14 * 4 * 4 * 6

Volume of a Cylinder = 301.44

**The Lateral Surface Area of a Cylinder** is

LSA = 2πrh

LSA = 2 * Math.PI * radius * height

LSA = 2 * 3.14 * 4 * 6

LSA = 150.72

**The Top Or Bottom Surface Area of a Cylinder** is

T = πr²

T = Math.PI * radius * radius

T = 3.14 * 4 * 4

T = 50.24

**NOTE:** For the calculation purpose we have taken π = 3.14 instead of (3.142857142..). So, All the above values are almost equal to program output but may differ at 0.01.

## Java Program to find Volume and Surface Area of a Cylinder using Functions

This Java program allows the user to enter the value of a radius and height. Using these values, we will calculate the Volume of a Cylinder, Lateral Surface Area of a Cylinder, Surface Area of a Cylinder and Top Or Bottom Surface Area of a Cylinder as per the formula. In this example, we are going to use the logic that we specified in first example but we will separated the logic and place it in a method.

package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCylinderUsingMethods { private static Scanner sc; public static void main(String[] args) { double radius, height; sc = new Scanner(System.in); System.out.println("\n Please Enter the radius of a Cylinder : "); radius = sc.nextDouble(); System.out.println("\n Please Enter the Height of a Cylinder : "); height = sc.nextDouble(); VolumeOfCylinder(radius, height); } public static void VolumeOfCylinder (double radius, double height) { // L = Lateral Surface Area of a Cylinder, T = Top Surface Area double sa, Volume, L, T; sa = 2 * Math.PI * radius * (radius + height); Volume = Math.PI * radius * radius * height; L = 2 * Math.PI * radius * height; T = Math.PI * radius * radius; System.out.format("\n The Surface area of a Cylinder = %.2f", sa); System.out.format("\n The Volume of a Cylinder = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L); System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T); } }

**OUTPUT**

## Java Program to find Volume and Surface Area of a Cylinder using Oops

In this Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

package SurfaceAreaPrograms; public class VolumeOfACylinder { double sa, Volume, L, T; public double VolumeOfCylinder (double radius, double height) { Volume = Math.PI * radius * radius * height; return Volume; } public double SurfaceAreaOfCylinder (double radius, double height) { sa = 2 * Math.PI * radius * (radius + height); return sa; } public double LateralSurfaceAreaOfCylinder (double radius, double height) { L = 2 * Math.PI * radius * height; return L; } public double TotalSurfaceAreaOfCylinder (double radius) { T = Math.PI * radius * radius; return T; } }

Within the Main program, we will create an instance of the above specified class and call the methods.

**JAVA CODE**

package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCylinderUsingClass { private static Scanner sc; public static void main(String[] args) { double radius, height, sa, Volume, L, T; sc = new Scanner(System.in); System.out.println("\n Please Enter the radius of a Cylinder : "); radius = sc.nextDouble(); System.out.println("\n Please Enter the Height of a Cylinder : "); height = sc.nextDouble(); VolumeOfACylinder vac = new VolumeOfACylinder(); sa = vac.SurfaceAreaOfCylinder(radius, height); Volume = vac.VolumeOfCylinder(radius, height); L = vac.LateralSurfaceAreaOfCylinder(radius, height); T = vac.TotalSurfaceAreaOfCylinder(radius); System.out.format("\n The Surface area of a Cylinder = %.2f", sa); System.out.format("\n The Volume of a Cylinder = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L); System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T); } }

**OUTPUT**

**ANALYSIS**

**AreaOfACylinder Class Analysis:**

- First we declared a function
**VolumeofCylinder**with two argument. Within the Java Program to find Volume and Surface Area of a Cylinder function, we are calculating the Volume of a Cylinder and returning the value - Next, we declared a function
**Surface****AreaofCylinder**with two argument. Within the function, we are calculating the Surface Area of a Cylinder and returning the value - Next we declared a function
**Lateral****Surface****AreaofCylinder**with two argument. Within the function, we are calculating the Lateral Surface Area of a Cylinder and returning the value - Next we declared a function
**Total****Surface****AreaofCylinder**with one argument. Within the function, we are calculating the Total Surface Area of a Cylinder and returning the value

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

In this Java Program to find Volume and Surface Area of a Cylinder, First we created an instance / created an Object of the **AreaOfACylinder** Class

VolumeOfACylinder vac = new VolumeOfACylinder();

Next, we are calling the **VolumeofCylinder** method. This is the first method that we created with double data type and this method will calculate Volume of a Cylinder and return a value so, we are assigning the return value to volume variable.

Volume = vac.VolumeOfCylinder(radius, height);

Next, we are calling the **Surface****AreaofCylinder** method. This is the second method that we created with double data type and this method will calculate Surface Area of a Cylinder and return a value so, we are assigning the return value to sa variable.

sa = vac.SurfaceAreaOfCylinder(radius, height);

Next, we are calling the **Lateral****Surface****AreaofCylinder** method. This is the third method that we created with double data type and this method will calculate Lateral Surface Area of a Cylinder and return a value so, we are assigning the return value to L variable.

L = vac.LateralSurfaceAreaOfCylinder(radius, height);

Next, we are calling the **Total****Surface****AreaofCylinder** method. This is the fourth method that we created with double data type and this method will calculate Total Surface Area of a Cylinder and return a value so, we are assigning the return value to T variable.

T = vac.TotalSurfaceAreaOfCylinder(radius);

Lastly we used following System.out.format statement to print the Volume of a Cylinder, Lateral Surface Area of a Cylinder, Surface Area of a Cylinder and Top Or Bottom Surface Area of a Cylinder

System.out.format("\n The Surface area of a Cylinder = %.2f", sa); System.out.format("\n The Volume of a Cylinder = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L); System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);