Professional Documents
Culture Documents
Chapte1 Arithmetic Extra
Chapte1 Arithmetic Extra
It
doesn’t matter which operator comes first. Go through the explanation and try to
understand.
Example :
If you have a / b % c * d :
• The step is : a/b first, then the answer % c, then the answer * d.
If you have a / (b % c) *d :
• Of course you have to do the ones in the brackets first.
• Then arrange the expression a / answer * d
• Next do a / answer first then only * d
For + and –
The calculation is from left to right. It doesn’t matter which operator comes first.
Example 1
int a = 1, b = 2, c = 3
if given the formula y = ++a – b + c--
y = ++ a – b + c--
++1 – 2 + 3
2–2+3
y=3
For * , / and %
The calculation is also from left to right. It doesn’t matter which operator comes first.
Example 1
int a = 1, b = 2, c = 3
if given the formula y = ++a % b * c--
y = ++ a % b * c--
++1 % 2 * 3
2%2*3
0 *3
0
NOTE : don’t do * first ----- it’s wrong. Do from left to right, follow the expression, do
% then only do *)
y=0*3
y=0
Example 2
int a = 15, b = 4, c = 2
if given the formula y = ++a / 3 % c * 2 % 3 * b++
y = ++a / 3 % c * 2 % 3 * b++
16 / 3 % 2 * 2 % 3 * 4
5 %2*2%3*4
1 *2%3*4
2 %3*4
2 *4
8
The example below shows you a combination of all arithmetic operators
int a = 15, b = 4, c = 2, p, q, r;
p = b / 3 % --a + c % 2;
q = c-- * a / (3 + b) % a;
r = (10 - c) + a % ++b * 2 - c;
p = b / 3 % --a + c % 2
= 4 / 3 % --15 + 2 % 2 (first step is to replace the variables with the values)
= 4 / 3 % 14 + 2 % 2 (next the ++ or -- is calculated. At this point a has
become 14 because of --a)
= 1 % 14 + 2 % 2
=1+2%2
=1+0
=1
Next expression:
q = c-- * a / (3 + b) % a
= 2-- * 14 / (3 + 4) % 14 (replace the variables with values)
= 2 * 14 / (3+4) % 14 (++ or -- is calculated. c is still 2 because of c--)
= 28 / 7 % 14
= 4 % 14
= 4
Next expression:
r = (10 - c) + a % ++b * 2 - c;
= (10 – 1) + 14 % ++4 * 2 – 1
= (10 – 1) + 14 % 5 * 2 – 1 (you cannot do * first, you must do from left to
right. So do % first)
= 9 + 4 *2–1
= 9 + 8 -1
= 16