- Write a java program to calculate the volume and surface area of sphere.

In this Java program, We have to find the volume and total surface of a cylinder, given the height and base radius of cylinder.

**Volume of Cylinder**

The volume of a right circular cylinder is defined as the amount of three dimensional space occupied by the cylinder or the storage capacity of a cylinder. To calculate the volume of a cylinder, we need radius of base height of cylinder.

**Volume of right circular cylinder**= Base area x Height

As base of cylinder is circular, Base Area = ΠR

^{2}

**Total Surface Area of Cylinder**

Surface area of cylinder is the number of square units that will exactly cover the outer surface of a cone. There are three surfaces in a cylinder, one curved and two circular bases. Total surface area of cylinder is the sum of the area of both circular bases and area of curved surface.

- Base area of cylinder = ΠR
^{2} - Curved surface area of cone = 2ΠRH

= 2ΠR

^{2}+ 2ΠRH

= 2ΠR(R + H)

NOTE : Π = 3.141

## Java program to find the volume and surface area of cylinder

In this java program, we first take base radius and height of cylinder as input from user and then calculate volume and surface area using above mentioned mathematical formulae.

package com.tcc.java.programs; import java.util.Scanner; /** * Java Program to find volume and surface area of Cylinder */ public class VolumeOfCylinder { public static void main(String[] args) { double radius, height, volume, surfaceArea; Scanner scanner; scanner = new Scanner(System.in); // Take input from user System.out.println("Enter Base Radius of Cylinder"); radius = scanner.nextDouble(); System.out.println("Enter Height of Cylinder"); height = scanner.nextDouble(); /* * Total surface area of Cylinder = 2 X PI X Radius X (Radius + Height) */ surfaceArea = 2 * Math.PI * radius * (radius + height); /* Volume of Cylinder = PI X Radius X Radius X Height */ volume = Math.PI * radius * radius * height; System.out.format("Surface Area of Cylinder = %.3f\n", surfaceArea); System.out.format("Volume of Cylinder = %.3f\n", volume); } }Output

Enter Base Radius of Cylinder 4 Enter Height of Cylinder 5 Surface Area of Cylinder = 226.195 Volume of Cylinder = 251.327

## Java program to find the volume and total surface area of cylinder using function

This java program is similar to above program except here we are using two user defined functions "getAreaOfCylinder" and "gerVolumeOfCylinder" functions to calculate total surface area and volume of cylinder respectively.

package com.tcc.java.programs; import java.util.Scanner; /** * Java Program to find volume and surface area of Cylinder using function */ public class VolumeOfCylinderFunction { public static void main(String[] args) { double radius, height; Scanner scanner; scanner = new Scanner(System.in); // Take input from user System.out.println("Enter Base Radius of Cylinder"); radius = scanner.nextDouble(); System.out.println("Enter Height of Cylinder"); height = scanner.nextDouble(); System.out.format("Surface Area of Cylinder = %.3f\n", getAreaOfCylinder(radius, height)); System.out.format("Volume of Cylinder = %.3f\n", gerVolumeOfCylinder(radius, height)); } public static double getAreaOfCylinder(double radius, double height) { /* * Total surface area of Cylinder = 2 X PI X Radius X (Radius + Height) */ return 2 * Math.PI * radius * (radius + height); } public static double gerVolumeOfCylinder(double radius, double height) { /* Volume of Cylinder = PI X Radius X Radius X Height */ return Math.PI * radius * radius * height; } }Output

Enter Base Radius of Cylinder 5 Enter Height of Cylinder 7 Surface Area of Cylinder = 376.991 Volume of Cylinder = 549.779

**Recommended Posts**