Java Program to Find Volume and Surface Area of Cone

This is the Java Program to Find the Surface Area and Volume of a Cone.

Problem Description

Given the dimensions of a cone, find out its surface area and volume.

Problem Solution

The surface area and volume of a cone can be calculated using the formulas:

Surface Area = (PI * radius * slant height) + (PI * radius2).
Volume = PI * radius2 * height/3.

Program/Source Code

Here is the source code of the Java Program to Find the Surface Area and Volume of a Cone. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Find the Surface Area and Volume of a Cone
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Cone {
  8.     // Function to calculate and print the surface area and volume of a cone
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         double radius,height;
  12.         System.out.println("Enter the radius and
  13.                             height of the right circular cone");
  14.         try{
  15.             radius=Double.parseDouble(br.readLine());
  16.             height=Double.parseDouble(br.readLine());
  17.         }catch (Exception e){
  18.             System.out.println("An error occurred");
  19.             return;
  20.         }
  21.         if(radius<=0 || height<=0){
  22.             System.out.println("Wrong Input");
  23.             return;
  24.         }
  25.         double slantheight = Math.sqrt(Math.pow(radius,2) + Math.pow(height,2));
  26.         System.out.println("Volume = " + (Math.PI*Math.pow(radius,2)*height/3));
  27.         System.out.println("Surface area = " + ((Math.PI*radius*slantheight)
  28.                                                  + (Math.PI*radius*radius)));
  29.     }
  30. }
Program Explanation

1. In function main(), first, the radius and the height of the cone are taken as input.
2. The condition if(radius<=0 || height<=0) checks if the input is valid or not.
3. The print statement displays the surface area and volume of the cone.

advertisement
advertisement

Time Complexity: O(1)

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the radius and height of the right circular cone
3.42
12
Volume = 146.98129725379061
Surface area = 170.81027853689216

Sanfoundry Global Education & Learning Series – Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.