Arithmetic Problems

Given the following declarations, what is the value of each expression?
int a = 2; int b = 4; int c = 6; float f = 5.0; float g = 8.0;

1. a + b - c 2. a + b * c 3. c / b 4. c % b 5. b % c
6. g / f 7. g / c 8. 12 / f 9. 4 / 3 * 3.14159 10. 12 - 18 % b / c + a
11. (5 + b / c) % 2 12. 5 * 3 % 7 + 8 / 3 - 6 13. (14 + 5) % 5 - 6 / 5.0 14. f / (10 % c) 15. 20 * a + (g - c)


Arithmetic Problem Solutions

Click on "explain" to see how the answer in red was calculated


1. a + b - c = 0

Explain

2. a + b * c = 26

Explain

3. c / b = 1

Explain

4. c % b = 2

Explain

5. b % c = 4

Explain

6. g / f = 1.6

Explain

7. g / c = 1.33333

Explain

8. 12 / f = 2.4

Explain

9. 4 / 3 * 3.14159 = 3.14159

Explain

10. 12 - 18 % b / c + a = 14

Explain

11. (5 + b / c) % 2 = 1

Explain

12. 5 * 3 % 7 + 8 / 3 - 6 = -3

Explain

13. (14 + 5) % 5 - 6 / 5.0 = 2.8

Explain

14. f / (10 % c) = 1.25

Explain

15. 20 * a + (g - c) = 42.0

Explain

Problem Explanations


Explanation of Problem 1

a + b - c is equivalent to 2 + 4 - 6. Since + and - have the same level of precedence, we perform them from left to right.
2 + 4 - 6 = 6 - 6 = 0

Explanation of Problem 2

By the rules of operator precedence, multiplication is performed before addtion, so 2 + 4 * 6 = 2 + (4 * 6) = 2 + 24 = 26

Explanation of Problem 3

c / b is an example of "integer division". Do the division, but ignore the remainder. In this case, 6 / 4 = 1 1/2, but throw away the 1/2 and the result is 1.

Explanation of Problem 4

The result from of mod operator (%) is the remainder after dividing.
6 / 4 = 1 with remainder of 2, so the value of 6 % 4 is 2.

Explanation of Problem 5

The result from of mod operator (%) is the remainder after dividing. 4 / 6 = 0 with remainder of 4, so the value of 4 % 6 is 4.

Explanation of Problem 6

g / f is equivalent to 8.0 / 5.0. Since both operands are floating point variables, the result is a floating point variable. 8.0 / 5.0 = 1.6

Explanation of Problem 7

g / c is equalvent to 8.0 / 6. Since one of the operands is floating point and one is an integer, the integer is changed to floating point and the result is floating point.... so 8.0 / 6 becomes 8.0 / 6.0 = 1.33333

Explanation of Problem 8

12 / f is equalvent to 12 / 5.0. Once again, since one of the operands is floating point and one is an integer, the integer is changed to floating point and the result is floating point.... 12 / 5.0 = 12.0 / 5.0 = 2.4

Explanation of Problem 9

Since division (/) and multiplication (*) have the same precedence, the operations are performed from left to right.
Since 4 and 3 are both integers, 4 / 3 is integer division and the result is 1. Then 1 * 3.14159 is an integer times a floating point so the result is floating point 3.14159.

Explanation of Problem 10

12 - 18 % b / c + a is quivalent to 12 - 18 % 4 / 6 + 2
Since % and / have higher precedence than - and +, they are performed first, from left to right.
First 18 % 4 is the remainder after dividing so 18 % 4 = 2. So we now have 12 - 2 / 6 + 2. Since 2 and 6 are integers, integer division id performed and 2 /6 = 0, which simplifies the problem to 12 - 0 + 2 = 14.

Explanation of Problem 11

(5 + b / c) % 2 is equivalent to (5 + 4 / 6) % 2.
Starting inside the parenthesis, we first perform the integer division 4 / 6, which is 0. Since 5 + 0 = 0, the problem simplifies to 5 % 2. The % operator is the remainder after divsion. 5 / 2 is 2 with remainder of 1, so 5 % 2 = 1.

Remeber that doing x % 2 tells us if x is odd or even. If x is odd, the result is 1. If x is even the result is 0.


Explanation of Problem 12

5 * 3 % 7 + 8 / 3 - 6
Since there are no parenethesis, and /, * and % have higher precedence than + and -, let's put parenthesis to indicate the order of operations:
( (5 * 3) % 7) + (8 / 3) - 6
In the first set of parentheis, 5 * 3 = 15 and then 15 % 7 = 1.
In the second set of parenthesis, 8 / 3 = 2 (integer division).
So the problem reduces to 1 + 2 - 6 = -3.

Explanation of Problem 13

(14 + 5) % 5 - 6 / 5.0
Parenthesis first, so 14 + 5 = 19, yielding 19 % 5 - 6 / 5.0
% and / have higher precedence than -, so first 19 % 5 = 4.
Second, 6 / 5.0 is mixed division (integer and floating point), so it becomes all floating point, so 6.0 / 5.0 = 1.2.
Our problem has reduced to 4 - 1.2 which is 2.8

Explanation of Problem 14

f / (10 % c) is equivalent to 5.0 / (10 % 6)
Inside the parenthesis, 10 % 6 = 4. Then 5.0 / 4 is mixed division (integer and float), so it becomes all float and
the result is of type float..... 5.0 / 4.0 = 1.25

Explanation of Problem 15

20 * a + (g - c) is equivalent to 20 * 2 + (8.0 - 6)
Inside the parenthesis, 8.0 - 6 becomes 8.0 - 6.0 which is 2.0
Our problem is then 20 * 2 + 2.0
Since * is higher precedence than +, we first multiply, then add.
20 * 2 = 40.... 40 + 2.0 = 42.0