Site icon TestingDocs.com

Volume of a Cylinder Java Program

Overview

In this post, we will write a java program to calculate the volume of the Cylinder.

IPO Chart

Input

r – Radius of the cylinder.

h = Height of the cylinder.

Process

Formula to calculate the volume of cylinder  V =  π * r2 * h

Output

The volume of the Cylinder

 

Tools Used

We will use the below tools to develop the program. 

Eclipse IDE

Some steps and instructions to create a Java application on Eclipse IDE:

https://www.testingdocs.com/create-a-new-java-project-in-eclipse/

https://www.testingdocs.com/create-java-package-in-eclipse-ide/

https://www.testingdocs.com/create-a-new-java-class-in-a-project/

https://www.testingdocs.com/run-java-project-in-eclipse/

Java Program

The program prompts the user to enter the height and the radius of the cylinder. The program uses the Scanner class to read the values from the keyboard. The variables are declared as double data types. 

The program calculates the volume as per the mathematical formula and displays the result to the screen. 

Steps

Launch IDE and create a Java class.

Enter the program code. 

/**************************************************
* CylinderVolume.java
* @program : Program to calculate the Cylinder volume.
* @web : www.TestingDocs.com
* @version : 1.0
**************************************************/
import java.util.Scanner;

public class CylinderVolume {

public static void main(String[] args) {
// scanner object variable used in the program
Scanner keyboard = new Scanner(System.in);

//Input
System.out.println("Enter Height of the cylinder :=");
double height = keyboard.nextDouble();

System.out.println("Enter radius of the cylinder :=");
double radius = keyboard.nextDouble();

// Process -Calculation
double volume=Math.PI*(radius*radius)*height;

// Output
System.out.println("Volume of the cylinder="+volume);

}
}

Screenshot

Screenshot the code in Eclipse IDE.

 

Save and Run the program from the IDE. Execute some test cases to verify the output of the program.  Right Click and choose Run as >> Java Application

Sample Output

Enter Height of the cylinder :=
5.5
Enter radius of the cylinder :=
3.5
Volume of the cylinder=211.6648050356123

 

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/


For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Exit mobile version