0

May I ask what I'm doing wrong here? I'm trying to calculate for the opposite side, given the angle and hypotenuse. I feel like I'm using sine the wrong way. Need some clarification on why my code isn't working.

#include <stdio.h>
#include <math.h>

int main () {
    
    double fAngle, fHyp;
    
    printf("Angle: ");
    scanf("%lf", &fAngle);

    printf("Hypotenuse: ");
    scanf("%lf", &fHyp);
    
    
    printf("The opposite side is %lf", sin(fAngle) * fHyp);
    
    return 0;

}
  • 3
    What is your input and output? Are you entering the angle in radians? – dbush Nov 09 '21 at 13:42
  • Unrelated: for some angles you will get a negative length; may want to use `fabs()` or "reduce" the angle. – pmg Nov 09 '21 at 13:46
  • I ran your program and entered an angle of 1.04719755133 (π/3) and a hypotenuse of 2. It gave me 1.732051, which looks about right for √3. – Steve Summit Nov 09 '21 at 14:06
  • @pmg Well, they are talking about hypotenuse, so I'd assume a right triangle and (but the program should check) 0 <= `fAngle` < π/2. – Bob__ Nov 09 '21 at 15:46
  • Tip: to improve quality of `sin()` when `fAngle` is some _large_ value, reduce with `fAngle = fmod(fAngle, 360.0);` before converting to radians. – chux - Reinstate Monica Nov 09 '21 at 18:40
  • 1
    Mere Pixel, Tip: post input used, output seen and output expected to improve the quality of a question. – chux - Reinstate Monica Nov 09 '21 at 18:42

1 Answers1

1

You most likely are entering input in degree angles, while your code expects radian angles.

You can easily convert to radians like this :

double fAngle;

printf("Angle: ");
scanf("%lf", &fAngle);
fAngle = fAngle * 2.0 * M_PI / 360.0

π radians are equal to 180°

sybog64
  • 159
  • 9