How would I type y = x + 3/4 - 2 in c++ lang.?

How would I type y = x + 3/4 - 2 in c++ lang.?
Depends what you are doing with it? What are you trying to do?
1
2
3
4
5
6
7
8
9
double x;
double y;

cout << "Type a decimal point value for x" << endl;
cin >> x;

y = x + 3.0/4.0 - 2.0;

cout << "The value of y is " << y << endl;


This is not a complete program - you need some #include files and a main function.

I think you need to do a lot of reading, try the reference & articles section at the top left of this page. Do some tutorials. Google stuff you are unsure of.

Good Luck
#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
int main()
{
int x , y;
cout<<"The value of \"x\" is ";
cin>>x;

y=(x+3)/(4-2);

cout << y;
}


x / 4 + 3 - 2 // according to order of the operators (if am not wrong)
Last edited on
@cppbeginer123 (8)

x / 4 + 3 - 2 // according to order of the operators (if am not wrong)


Your parentheses enforce a different order of evaluation than the normal mathematical precedence, which is BEDMAS - Brackets, exponents, division, multiplication, addition, subtraction.

So the result of x + 3.0/4.0 - 2.0; is x + 0.75 - 2.0.

One shouldn't infer any other interpretation because varying results can be obtained by altering the parentheses, so one can only take the OP's expression at face value.


Edit: Pleas always use code tags - the <> button on the right.
Last edited on
Topic archived. No new replies allowed.