-2

I have a homework in C++ Intro. and one of it said to convert the following mathematical equation into a C++ code:

x = 10π/(a+b) sin3C+ 3(ln a)(tan C)

where a,b, and C are user-input and C is in degrees.

I've tried it myself and had end up with this:

float x,y,z,a,b,C;

cout<< "Input the a-value: ";
cin>> a;

cout<< "\nInput the b-value: ";
cin>> b;

cout<< "\nInput the C-value: ";
cin>> C;
C = C*3.1416/180;

x = (10*3.1416/a+b)*pow(sin(C),3)+3*log(a)*tan(C);
cout<< "\n The value of x is " << x;

I've tried a=5,b=10, and C=15 and the result of x is 1.57606. I've tried it in a scientific calculator and x became 1.33005. What could be the problem in my code? Thank you!

I'm sorry if there's anything wrong with my post structure because it's my first time to post here and English is not my native language

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
marshblocker
  • 83
  • 1
  • 6
  • 4
    Note that you didn't respect the parenthesis in `(a+b)` from the original equation. And I don't see the exponent in the original equation that is implied by the `pow` in your code, but that may be a formatting issue. – François Andrieux Jan 30 '19 at 15:47
  • Please take more care when copy-pasting the equation from your homework. Clearly you intended `sin³(C)`, so please ensure that the question reflects that. – Max Langhof Jan 30 '19 at 16:06
  • I'm really sorry. I'll take note of that in the future. Thank you for the reply! – marshblocker Jan 30 '19 at 16:58

1 Answers1

5
(10*3.1416/a+b)

is not the same as 10π/(a+b), you meant

(10*3.1416/(a+b))
john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    Also note that π is not `3.1416`. See [here](https://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c) for what you can do instead. – Max Langhof Jan 30 '19 at 16:05
  • Oh wow, I guess you're right. I'm really sorry, such foolish mistake. Thanks you! – marshblocker Jan 30 '19 at 16:59