JavaScript Program to Find the Tangent of given Radian Value
Given a radian value, our task is to find a tangent using JavaScript. Tangent (tan) is a trigonometric function which is the ratio of the length of the side opposite to an angle to the length of the adjacent side in a right triangle.
Example:
Input: 45 degrees
Output: 1
Below are the approaches to find the tangent of a given radian value using JavaScript:
Table of Content
- Using Math.tan() Function
- Using Trigonometric Formula
Using Math.tan() Function
Create a function that takes a radian parameter. Inside the function, use the Math.tan() function provided by JavaScript’s Math object to calculate the tangent of the given radian value. Return the calculated tangent value.
Example: The example below shows finding the tangent of a given radian value Using Math.tan() Function.
// Define the function
function findTangent(radian) {
return Math.tan(radian);
}
const radianValue = 60;
console.log("Tangent of", radianValue,
"radians is:",
findTangent(radianValue));
Output
Tangent of 60 radians is: 0.320040389379563
Time complexity: O(1).
Space complexity: O(1).
Using Trigonometric Formula
Create a function that takes a radian parameter. Inside the function, use the Math.sin() function to calculate the sine of the given radian value and use the Math.cos() function to calculate the cosine of the radian value. Divide the sine value by the cosine value to obtain the tangent of the given radian value. Return the calculated tangent value.
Example: The example below shows finding the tangent of a given radian value Using Math.tan() Function.
// Define the function
function findTangent(radian) {
// Calculate the sine and cosine of the radian value
const sineValue = Math.sin(radian);
const cosineValue = Math.cos(radian);
// Calculate the tangent using formula
const tangentValue = sineValue / cosineValue;
return tangentValue;
}
const radianValue = 45;
console.log("Tangent of", radianValue,
"radians is:",
findTangent(radianValue));
Output
Tangent of 45 radians is: 1.6197751905438615
Time complexity: O(1).
Space complexity: O(1).
Contact Us