![]() |
We will learn how to find the volume of a cone using JavaScript. A cone is a three-dimensional geometric shape with a circular base and a pointed top. We will use the formula to return the volume of a cone. Formula Usedvolume of a cone is: (1/3) × π × r where:
Using FunctionCreate a function that takes the radius and height of the cone as input parameters. Inside the function, we will calculate volume of cone using the formula for volume of cone : (1/3) × π × r2 × h. Call the function and return the calculated volume of cone. Example: To demonstrate finding Volume of Cone using function.
Output Volume of the cone is: 201.06192982974676 Time Complexity: O(1) Space Complexity: O(1) Using ClassWe will define a class named Cone with a constructor method to initialize the radius and height of the cone, and we will define a method to calculate the volume of cone using the formula : (1/3) × π × r2 × h. Then we create an instance of the Cone class, pass the radius and height as arguments to the constructor. Call the method and print the result. Example: The example below shows the demonstration of finding volume of cone using class.
Output Volume of the cone : 201.06192982974676 Time Complexity: O(1) Space Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |