Operator Precedence

I got a problem that says that if the User enters 2,5,4.3, and 6 the output display for value 1 is 6, value 2 is 27, value 3 is 20.49, and value 4 is 38. I just need help on how each value came to be cause my calculation is completely different. For example, for value 2, I had 2 * 2^2 and it equals 8, but the correct answer is 6. What am I doing wrong here?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
#include <cmath>
using namespace std;

int main ()
{

double value1, value2, value3;
cout << "Enter a number:";
cin >> value1 ;
value2 = 2 * pow(value1, 2);
value3 = 3 + value2 /2-1;
cout << value3;

return 0;
}
I'm sorry value 2 should be 27 not 6 that I put on the last sentence.
Could you please clarify your question? It's a bit ambiguous.

Enter a number:2
6
1
2
3
4
5
6
7
value1 is 2.
value2 = 2 * 22
       = 8
value3 = 3 + 8 / 2 - 1
       = 3 + 4 - 1
       = 6
which is what we got



Enter a number:5
27
1
2
3
4
5
6
7
value1 is 5.
value2 = 2 * 52
       = 50
value3 = 3 + 50 / 2 - 1
       = 3 + 25 - 1
       = 27
which is what we got
Last edited on
Topic archived. No new replies allowed.