Horje
JavaScript Program to Find Surface Area of a Cone

Mathematically, the Surface area of the Cone can be calculated using the formula: area = pi * r * s + pi * r^2, Where r is the radius of the circular base, and s is the slant height of the cone. We will calculate the Surface Area of a Cone programmatically using JavaScript.

These are the following methods:

Using Destructuring Assignment and Spread Operator

In this approach, we are using the destructuring assignment and the spread operator to encapsulate the sum of the squares of radius and height within an array, allowing Math.sqrt to extract the value for calculating the surface area of the cone.

Syntax:

let [, value] = [...[expression]];

Example: The below uses Destructuring Assignment and Spread Operator to find the Surface area of a Cone.

Javascript
let r = 5;
let h = 10;
let res = Math.PI * r * (r + Math.sqrt([...[r*r + h*h]]));
console.log(res.toFixed(2));

Output
254.16

Using Arithmetic Operations

In this approach, we calculate the surface area of a cone using basic arithmetic operations. The formula 3.1415 * r * (r + √(r² + h²)) is used, providing the result with two decimal places using toFixed().

Example: The below uses Arithmetic Operations to find the Surface area of a Cone.

Javascript
let r = 5;
let h = 10;
let res = 3.1415 * r * (r + (r*r + h*h)**0.5);
console.log(res.toFixed(2));

Output
254.15

Using Math.pow

In this approach, we are using the Math.pow function for exponentiation, applying it to the expression (r * r + h * h) to find the square root. The calculated result is then used in the formula π * r * (r + √(r² + h²)), with the final surface area displayed with two decimal places using toFixed.

Syntax:

Math.pow(base, exponent);

Example: The below uses Math.pow to find the Surface area of a Cone.

Javascript
let r = 5;
let h = 10;
let res = Math.PI * r * (r + Math.pow((r * r + h * h), 0.5));
console.log(res.toFixed(2));

Output
254.16



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript if else else if JavaScript if else else if
How to Remove Focus from Input Field in TypeScript ? How to Remove Focus from Input Field in TypeScript ?
JavaScript Program for Sum of Digits of a Number using Recursion JavaScript Program for Sum of Digits of a Number using Recursion
How to Use a Typescript Switch on Multiple Inputs ? How to Use a Typescript Switch on Multiple Inputs ?
JavaScript Program to Reverse a String Using Recursion JavaScript Program to Reverse a String Using Recursion

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14