🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The tan() function in C is a standard library function that computes the tangent of a given angle. It is part of the C standard library (math.h). This function is useful for performing trigonometric calculations involving angles.
Table of Contents
- Introduction
tan()Function Syntax- Understanding
tan()Function - Examples
- Computing Tangent of an Angle
- Using
tan()with User Input
- Real-World Use Case
- Conclusion
Introduction
The tan() function computes the tangent of a given angle (in radians). The tangent function is a fundamental trigonometric function that returns the ratio of the length of the opposite side to the length of the adjacent side in a right-angled triangle.
tan() Function Syntax
The syntax for the tan() function is as follows:
#include <math.h>
double tan(double x);
Parameters:
x: The angle in radians for which the tangent is to be computed.
Returns:
- The function returns the tangent of the angle
x.
Understanding tan() Function
The tan() function takes an angle in radians as input and returns the tangent of that angle. To convert degrees to radians, use the formula:
[ \text{radians} = \text{degrees} \times \frac{\pi}{180} ]
Examples
Computing Tangent of an Angle
To demonstrate how to use tan() to compute the tangent of an angle, we will write a simple program.
Example
#include <stdio.h>
#include <math.h>
int main() {
double angle = M_PI / 4; // 45 degrees in radians
// Compute the tangent of the angle
double tangent_value = tan(angle);
// Print the result
printf("Tangent of %.2f radians (45 degrees) is: %.2f\n", angle, tangent_value);
return 0;
}
Output:
Tangent of 0.79 radians (45 degrees) is: 1.00
Using tan() with User Input
This example shows how to use tan() to compute the tangent of an angle provided by the user.
Example
#include <stdio.h>
#include <math.h>
int main() {
double degrees, radians;
// Get user input for the angle in degrees
printf("Enter the angle in degrees: ");
scanf("%lf", °rees);
// Convert degrees to radians
radians = degrees * (M_PI / 180.0);
// Compute the tangent of the angle
double tangent_value = tan(radians);
// Print the result
printf("Tangent of %.2f degrees is: %.2f\n", degrees, tangent_value);
return 0;
}
Output (example user input "45"):
Enter the angle in degrees: 45
Tangent of 45.00 degrees is: 1.00
Real-World Use Case
Calculating the Slope of a Line
In real-world applications, the tan() function can be used to calculate the slope of a line given its angle of inclination.
Example: Calculating the Slope of a Line
#include <stdio.h>
#include <math.h>
int main() {
double angle_degrees, angle_radians, slope;
// Get user input for the angle of inclination in degrees
printf("Enter the angle of inclination in degrees: ");
scanf("%lf", &angle_degrees);
// Convert angle to radians
angle_radians = angle_degrees * (M_PI / 180.0);
// Calculate the slope using the tangent function
slope = tan(angle_radians);
// Print the result
printf("The slope of the line is: %.2f\n", slope);
return 0;
}
Output (example user input angle "45"):
Enter the angle of inclination in degrees: 45
The slope of the line is: 1.00
Conclusion
The tan() function is essential for computing the tangent of an angle in C. It is useful in various trigonometric calculations, particularly in fields like geometry, physics, and engineering.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment