Can someone check if I am doing these right please, thanks ?

int a=3, b=5, x;
x= a/b + b/a +a%b + b%a; cout << x;    //  x=0+1+0+1= 2
y=float(b/a)+ float(b)/a; cout << y;   //  y=1.6+1=2.6
z=a*b/2; cout << z;                    //  z= 4
w=pow(b, a)+sqrt(a+b);  cout << w;     //  w=125+2sqrt(2)



int a=4, b=7, x; 
a++; --b;
x=a+b; cout<<"x"<< x;        //x=11

a=4; b=8
x=a++*--b; cout<<"x="<< x;   //x=35

a=3; b=5;
a+=3+b; b*=2;
x=a+b; cout<< "x="<< x;      //x=13

Recommended Answers

All 3 Replies

What happens if you run the code? I can tell you 5%3 is 2 not 1. % works as 5 - ((5/3) * 3) or a%b = a - ((a/b) * b). Line 4 should be 7 based on order of operations.

If the comments you added are supposed to be the answers you want, then no they are not all right. Line 11 gives the right answer the rest give different answers

Line 3, 4 and 5 needs declaration y, z and w. otherwise, with declaration, line 3 will give 1 since the operands are 'int'. line 4 will not give 4. it will give 6 or 7 depending on the precidence. line 5 will give 127 or 127.2828 depending on the declaration of w.
Line 18 has an out put of 28 becouse, a was increased after the operation while b is decreased before.
In line 18, the out come is 21 since a=a+3+b and b=b*2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.