Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java Program to find Volume and Surface Area of a Cylinder

by suresh

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);
	}
}

OUTPUT

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

ANALYSIS

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);
	}
}

OUTPUT

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

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);
	}
}

OUTPUT

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

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);

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy