×

Java Programs

Java Practice

Java program to find the Surface Area and volume of the cylinder

Given the values of radius and height, we have to find the Surface Area and volume of the cylinder.
Submitted by Nidhi, on February 27, 2022

Problem statement

In this program, we will read radius, height from the user and calculate the area, volume of the cylinder.

Source Code

The source code to find the Surface Area and volume of the cylinder is given below. The given program is compiled and executed successfully.

// Java program to find the Surface Area 
// and volume of the cylinder

import java.util.Scanner;

public class Main {
  static float calcuateVolumeOfCylinder(float radius, float height) {
    float result = 0.0F;
    result = 3.14F * radius * radius * height;
    return result;
  }

  static float calcuateAreaOfCylinder(float radius, float height) {
    float result = 0.0F;
    result = 2 * 3.14F * radius * (radius + height);
    return result;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    float radius = 0;
    float height = 0;
    float volume = 0;
    float area = 0;

    System.out.printf("Enter the radius: ");
    radius = SC.nextFloat();

    System.out.printf("Enter the height: ");
    height = SC.nextFloat();

    volume = calcuateVolumeOfCylinder(radius, height);
    area = calcuateAreaOfCylinder(radius, height);

    System.out.printf("Volume of Cylinder is: %f\n", volume);
    System.out.printf("Surface area of Cylinder is: %f\n", area);
  }
}

Output

Enter the radius: 3
Enter the height: 7
Volume of Cylinder is: 197.820007
Surface area of Cylinder is: 188.399994

Explanation

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contain three static methods calcuateVolumeOfCylinder(), calcuateAreaOfCylinder() and main().

The calcuateVolumeOfCylinder(), calcuateAreaOfCylinder() methods are used to calculate the volume and area of the Cylinder based on given radius and height.

The main() method is an entry point for the program. And, we read radius, height from the user using the Scanner class. Then we calculated the volume, area of the Cylinder using calcuateVolumeOfCylinder(), calcuateAreaOfCylinder() methods, and printed the result.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.