C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Volume of a Cylinder - Java Program

Formula

               Volume = π × r2 × h  

Where:

  • r = radius of the cylinder
  • h = height of the cylinder
  • Use π = 3.14159 (or Math.PI)

 

import java.util.Scanner;

 

public class CylinderVolume {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Taking input from user

        System.out.print("Enter radius of the cylinder: ");

        double radius = sc.nextDouble();

 

        System.out.print("Enter height of the cylinder: ");

        double height = sc.nextDouble();

 

        // Calculating the volume

        double volume = Math.PI * radius * radius * height;

 

        // Displaying the result

        System.out.println("Volume of the Cylinder = " + volume);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter radius of the cylinder: 5
Enter height of the cylinder: 10
Volume of the Cylinder = 785.3981633974483 

OUTPUT 2:
Enter radius of the cylinder: 3.5
Enter height of the cylinder: 8
Volume of the Cylinder = 307.8760800517997

Explanation

Step 1: Input radius and height

User enters:

double radius = sc.nextDouble();

double height = sc.nextDouble();

Step 2: Apply formula

Cylinder volume formula:

        V = πr2h

In Java:

double volume = Math.PI * radius * radius * height;

  • Math.PI provides an accurate value of π
  • radius * radius gives r2

Step 3: Display output

The final volume is printed:

System.out.println("Volume = " + volume);