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

Dani AI

Generated

Clarifications and corrected evaluations (building on points made by , and ):

Main pitfalls: integer division truncates before any cast, casting after an integer division does not recover the fractional part, % gives the integer remainder, pow/sqrt return floating-point, and pre/post ++/-- change values differently. Declarations and types matter (declare y, z, w with the type you expect).

With a = 3, b = 5:

  • a/b is 0 (integer division).
  • b/a is 1.
  • a % b is 3 (3 % 5 == 3).
  • b % a is 2 (5 % 3 == 2).
    So the sum of those four terms is 6 (not 2).

For the same a,b:

  • float(b/a) converts the integer result of b/a (which is 1) to 1.0.
  • float(b)/a does a floating-point division and is ~1.6666667.
    So the total is ~2.6666667. To get fully floating-point math, cast before dividing (for example, static_cast<double>(b)/a).

a*b/2 with integers is 15/2 -> 7 (integer truncation). If a floating result is desired, use a floating operand so the division is done in float/double.

pow(5,3) + sqrt(8) = 125 + 2.828427... = 127.828427... If w is an int that becomes 127 after truncation.

Other snippets:

  • Starting a=4, b=7, after a++; --b -> a==5, b==6, so x==11.
  • With a=4, b=8, x = a++ * --b evaluates to 4 * 7 = 28; final a==5, b==7.
  • With a=3, b=5, a += 3 + b -> a==11; b *= 2 -> b==10; so x == 21.

Practical rules: declare explicit types, cast before the division when a floating result is needed, add parentheses for clarity, and avoid modifying the same variable multiple times inside one expression.

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.