sin()

We will get sin value of any number by using sin() function.
sin(x) 
x = angular value in radian ( not in degree ) We will declare PI as constant as radian input value.

Example sin()

#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main(void){
     float  x,ret;
     x=90; // input in degree 
     ret=sin(x*PI/180);
     printf("sing(x)=%.1f", ret);
     return 0;
}
The above code will give this output.
sin(x)= 1.0

Example cos()

#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main(void){
     float  x,ret;
     x=180;
     ret=cos(x*PI/180);
     printf("cos(%.0f)=%.2f",x, ret);
     return 0;
}
Output
cos(180)=-1.00

Example tan()

#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main(void){
     float  x,ret;
     x=45;
     ret=tan(x*PI/180);
     printf("tan(%.0f)=%.2f",x, ret);
     return 0;
}
Output
tan(45)=1.00

Example: Trigonometric Functions with User Input

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main(void) {
    float angle, result;
    char function;
    
    printf("Enter angle in degrees: ");
    scanf("%f", &angle);
    
    printf("Enter 's' for sin, 'c' for cos, 't' for tan: ");
    scanf(" %c", &function);
    
    if (function == 's') {
        result = sin(angle * PI / 180);
        printf("sin(%.0f) = %.2f\n", angle, result);
    } else if (function == 'c') {
        result = cos(angle * PI / 180);
        printf("cos(%.0f) = %.2f\n", angle, result);
    } else if (function == 't') {
        result = tan(angle * PI / 180);
        printf("tan(%.0f) = %.2f\n", angle, result);
    } else {
        printf("Invalid function input.\n");
    }
    
    return 0;
}
Output:
Enter angle in degrees: 45
Enter 's' for sin, 'c' for cos, 't' for tan: s
sin(45) = 0.71

Example: Error Handling for Special Cases

#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main(void) {
    float angle, result;
    
    printf("Enter angle in degrees: ");
    scanf("%f", &angle);
    
    if ((int)angle % 90 == 0 && ((int)angle / 90) % 2 != 0) {
        printf("tan(%.0f) is undefined\n", angle);
    } else {
        result = tan(angle * PI / 180);
        printf("tan(%.0f) = %.2f\n", angle, result);
    }
    
    return 0;
}
Output:
Enter angle in degrees: 90
tan(90) is undefined


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer