How to write a java program to calculate the total surface area of a cylinder? Well! That’s quite simple here we share in three different ways to write Java code for the total surface area of a cylinder. Soon online execution tool is going to be embedded for each program such that you can execute here itself. In case if you need to know any more information about the following program, do comment here at the end of the post. Check out the table of contents.
Table Of Contents:Â
[table id=2 /]
Q: How to calculate the total surface area of a cylinder?
A: The total surface area of the cylinder includes a area of the circular top + curved surface area.
Here is the code in three different ways to calculate TSA
The formula to calculate the total surface area of the cylinder in math?
[wp_ad_camp_3]
Java code: Example – 1 ( Standard values )
Here we use simple standard values by using the above formula. For any mathematical oriented Java program, you can use the following format to execute. Below we do cover in more than two methods with outputs and online execution tool.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Scanner; class TotalSurfaceAreaOfCylinder { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the radius:"); double r=s.nextDouble(); System.out.println("Enter the height:"); double h=s.nextDouble(); double tsa=((2*22*r)/7)*(r+h); System.out.println("TotalSurfaceArea Of Cylinder is: " + tsa); } } |
1 2 3 |
Enter the radius:7.0 Enter the height:7.0 TotalSurfaceArea Of Cylinder is: 616.0 |
Java Code For TSA: 2 ( Command Line Arguments )
Here we use command line arguments. If you have no idea about java command line arguments, then do check out the guide. Command line arguments, where inputs displayed on the output console itself. Check out how the output will show alike.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class TotalSurfaceAreaOfCylinder1 { public static void main(String args[]) { double r=Double.parseDouble(args[0]); double h=Double.parseDouble(args[1]); double tsa=((2*22*r)/7)*(r+h); System.out.println("TotalSurfaceArea Of Cylinder is: " + tsa); } } |
1 2 3 |
javac TotalSurfaceAreaOfCylinder1.java java TotalSurfaceAreaOfCylinder1 7 7 TotalSurfaceArea Of Cylinder is: 616.0 |
Java Code TSA: 3 ( Return Invoke Method )
[wp_ad_camp_3]
( Updated )We got a mail from one user asked, is there any way on how to write a java program other than using static and command line arguments? Here we go another method using return to invoke a method. We do implement this on all our basic formula oriented programs. Do check it out 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; class TotalSurfaceAreaOfCylinder { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the length of Cubiod:"); double r=s.nextDouble(); System.out.println("Enter the breadth of Cubiod:"); double h=s.nextDouble(); double tsa=TotalSurfaceAreaOfCylinder.area(r,h); System.out.println("TotalSurfaceArea Of Cylinder is: " + tsa); } public static double area(double r,double h) { double a= ((2*22*r)/7)*(r+h); return a; } } |
1 2 3 |
Enter the radius:7.0 Enter the height:7.0 TotalSurfaceArea Of Cylinder is: 616.0 |
Check Out More Tutorials:Â