Java Program To Calculate And Display The Total Surface Area Of A Cylinder

Java Program To Calculate And Display The Total Surface Area Of A Cylinder

Formula to calculate the Total Surface Area of a Cylinder = (2*pi*r*r)+(2*pi*r*h)

Where,
r is the radius of the cylinder
h is the height of the cylinder

Program 1: Using Java Scanner Class

/*
Algorithm:
------------
1. Get the radius of the cylinder from the user.
2. Get the height of the cylinder from the user.
3. Calculate the Total Surface Area of the Cylinder.
4. Print the Total Surface Area of the Cylinder.
*/

//importing java scanner class
import java.util.Scanner;

public class TSAOfCylinder1{
  public static void main (String[] args){

    //creating new Scanner
    Scanner sc = new Scanner (System.in);

    System.out.println ("Please enter the radius of the cylinder :");
    double radius = sc.nextDouble();

    System.out.println ("Please enter the height of the cylinder :");
    double height = sc.nextDouble();

    //calculate tsa of the cylinder
    double pi = 3.14;
    double tsa = ((2*pi*radius*radius)+(2*pi*radius*height));

    System.out.println ("Total Surface Area of the Cylinder is : "+tsa);
  }
}

Save this file as 'TSAOfCylinder1.java'
Compile: $javac TSAOfCylinder1.java
Execute: $java TSAOfCylinder1
Output:

Please enter the radius of the cylinder :
3
Please enter the height of the cylinder :
8
Total Surface Area of the Cylinder is : 207.24

Program 2: Using Java BufferefReader Class

/*
Algorithm:
------------
1. Get the radius of the cylinder from the user.
2. Get the height of the cylinder from the user.
3. Calculate the Total Surface Area of the Cylinder.
4. Print the Total Surface Area of the Cylinder.
*/

//importing java bufferedreader class
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class TSAOfCylinder2{
  public static void main (String[] args)
  throws IOException{

    //creating new InputStreamReader
    InputStreamReader is = new InputStreamReader (System.in);

    //creating new BufferedReader
    BufferedReader br = new BufferedReader (is);

    System.out.println ("Please enter the radius of the cylinder :");
    double radius = Double.parseDouble (br.readLine());

    System.out.println ("Please enter the height of the cylinder :");
    double height = Double.parseDouble (br.readLine());

    //calculate tsa of the cylinder
    double pi = 3.14;
    double tsa = ((2*pi*radius*radius)+(2*pi*radius*height));

    System.out.println ("Total Surface Area of the Cylinder is : "+tsa);
  }
}

/*
Save this file as 'TSAOfCylinder2.java'
Compile: $javac TSAOfCylinder2.java
Execute: $java TSAOfCylinder2
Output:

Please enter the radius of the cylinder :
4
Please enter the height of the cylinder :
3

Total Surface Area of the Cylinder is : 175.84

Previous: Java Program To Calculate And Display The Area Of An Ellipse
Previous: How To Install Zorin OS 15.2 Core In 32-bit Or 64-bit System

Post a Comment

1 Comments