sin(), cos() and tan() calculate trigonometric values for an angle measured in radians. Convert degrees to radians before calling them.
double sin(double radians);
double cos(double radians);
double tan(double radians);
#include <math.h>
#include <stdio.h>
int main(void) {
const double pi = 3.14159265358979323846;
double degrees = 30.0;
double radians = degrees * pi / 180.0;
printf("sin(30) = %.3f\n", sin(radians));
printf("cos(30) = %.3f\n", cos(radians));
printf("tan(30) = %.3f\n", tan(radians));
return 0;
}
Output
sin(30) = 0.500
cos(30) = 0.866
tan(30) = 0.577
#include <math.h>
#include <stdio.h>
int main(void) {
const double pi = 3.14159265358979323846;
double angle = pi / 2.0;
printf("sin(pi/2) = %.1f\n", sin(angle));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
const double pi = 3.14159265358979323846;
double degrees;
char choice;
printf("Enter angle in degrees: ");
if (scanf("%lf", °rees) != 1) {
printf("Invalid angle.\n");
return 1;
}
printf("Enter s for sin, c for cos, or t for tan: ");
if (scanf(" %c", &choice) != 1) {
return 1;
}
double radians = degrees * pi / 180.0;
if (choice == 's') {
printf("sin(%.2f) = %.6f\n", degrees, sin(radians));
} else if (choice == 'c') {
printf("cos(%.2f) = %.6f\n", degrees, cos(radians));
} else if (choice == 't') {
if (fabs(cos(radians)) < 1e-12) {
printf("tan(%.2f) is undefined.\n", degrees);
} else {
printf("tan(%.2f) = %.6f\n", degrees, tan(radians));
}
} else {
printf("Unknown choice.\n");
return 1;
}
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
const double pi = 3.14159265358979323846;
double degrees = 90.0;
double radians = degrees * pi / 180.0;
if (fabs(cos(radians)) < 1e-12) {
printf("tan(90) is undefined.\n");
} else {
printf("tan(90) = %.6f\n", tan(radians));
}
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.