Java Program to find Volume and Surface Area of a Cylinder

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 the Surface Area of a Cylinder, Top Or Bottom Surface Area, Lateral Surface Area and Volume of a Cylinder.

Java 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).

The Java Volume of a Cylinder

The amount of space inside the Cylinder is called Volume. If we know the height of a cylinder then we can calculate the volume of a cylinder using the 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. Next, the Java program 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);
	}
}
Java Program to find Volume and Surface Area of a Cylinder 1

The following statements will allow the user to enter the radius and height of a cylinder. 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 Java line, We are calculating the volume 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;

The following System.out.format statements will help us 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);

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. Next, this Java program 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 use the logic that we specified in the first example but 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);
	}
}
 Please Enter the radius of a Cylinder : 
3

 Please Enter the Height of a Cylinder : 
5

 The Surface area of a Cylinder = 150.80
 The Volume of a Cylinder = 141.37
 The Lateral Surface area of a Cylinder = 94.25
 The Top OR Bottom Surface Area of a cylinder = 28.27

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

In this Java program, we are dividing the Volume and Surface Area of a Cylinder code using the Object-Oriented Programming. To do this, we will create a class that holds 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 Java Program to find Volume and Surface Area of a Cylinder program, we will create an instance of the above-specified class and call the methods.

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);
	}
}
 Please Enter the radius of a Cylinder : 
5

 Please Enter the Height of a Cylinder : 
9

 The Surface area of a Cylinder = 439.82
 The Volume of a Cylinder = 706.86
 The Lateral Surface area of a Cylinder = 282.74
 The Top OR Bottom Surface Area of a cylinder = 78.54

AreaOfACylinder Class Analysis:

  1. 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.
  2. Next, we declared a function SurfaceAreaofCylinder with two arguments. Within the function, we are calculating the Surface Area of a Cylinder and returning the value.
  3. Next, we declared a function LateralSurfaceAreaofCylinder with two arguments. Within the function, we are calculating the Lateral Surface Area of a Cylinder and returning the value.
  4. Here, we declared a function TotalSurfaceAreaofCylinder with one argument. Within the function, we are calculating the Total Surface Area of a Cylinder and returning the value.

Main Class Analysis:

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

VolumeOfACylinder vac = new VolumeOfACylinder();

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

Volume = vac.VolumeOfCylinder(radius, height);

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

sa = vac.SurfaceAreaOfCylinder(radius, height);

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

L = vac.LateralSurfaceAreaOfCylinder(radius, height);

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

T = vac.TotalSurfaceAreaOfCylinder(radius);

Lastly, we used the following System.out.format statement to print the Java 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);