🇯🇵 日本語 | 🇺🇸 English

Quiz (Arithmetic)

Interactive quiz on C arithmetic operators.

Question 1 — int / int division

Predict the output.
int a = 7, b = 5;
printf("a / b = %d\n", a / b);

What does this print?

a / b = 1.4
a / b = 1
a / b = 2
a / b = 1.400000
Explanation: int / int produces an int (integer division).
7 / 5 is 1.4 mathematically, but the fractional part is truncated, leaving 1.
Remember: integer division always truncates.

Question 2 — Mixing int and double

int a = 7;
double b = 5.0;
printf("a / b = %f\n", a / b);

What does this print?

a / b = 1.400000
a / b = 1.000000
a / b = 1
Explanation: When int and double are mixed, the int is automatically promoted to double (implicit conversion).
7.0 / 5.0 = 1.4 → printed as 1.400000 with %f.
If either operand is a double, the result is a double.

Question 3 — (double) cast

int a = 7, b = 5;
printf("a / b = %f\n", (double)a / b);

What does this print?

Error
a / b = 1.000000
a / b = 1.400000
Explanation: (double)a explicitly casts a to double. The division then becomes double / int, and b is promoted to double as well.
7.0 / 5.0 = 1.4 → 1.400000.
Casting one operand is how you get floating-point division from two int variables.

Question 4 — Modulo operator (%)

int a = 17, b = 5;
printf("%d / %d = %d remainder %d\n", a, b, a/b, a%b);

What does this print?

17 / 5 = 3 remainder 2
17 / 5 = 3.4 remainder 0
17 / 5 = 2 remainder 3
Explanation: a / b = 17 / 5 = 3 (truncated), and a % b = remainder = 2.
% returns the remainder and only works on integer types — not on doubles.
Common uses include even/odd checks (e.g. n % 2 == 0) and extracting digits.

Question 5 — Operator precedence

As in math, * and / are evaluated before + and -.
int a = 2 + 3 * 4;
printf("a = %d\n", a);

What does this print?

a = 20
a = 14
a = 24
a = 9
Explanation: In C, as in math, * / % have higher precedence than + -.
(1) 3 * 4 = 12 is evaluated first.
(2) 2 + 12 = 14.
Evaluation isn't strictly left-to-right — higher-precedence operators run first.

Question 6 — Changing order with parentheses

Parentheses raise the precedence of whatever is inside them.
int a = (2 + 3) * 4;
printf("a = %d\n", a);

What does this print?

a = 14
a = 24
a = 20
a = 5
Explanation: Expressions inside ( ) are evaluated first.
(1) (2 + 3) = 5.
(2) 5 * 4 = 20.
When you're unsure about precedence, add explicit parentheses — it also makes the code easier to read.

Question 7 — Using % on a double

Does the modulo operator work on any type?
double a = 7.0, b = 2.0;
printf("%f\n", a % b);

What happens?

Prints 1.000000
Compile error
Prints 0.000000
Prints 3.500000
Explanation: The % operator only works on integer types. Using it on double values is a compile error.
For floating-point remainders, include <math.h> and use fmod(a, b) instead.

Question 8 — Division with negative numbers

What happens when you divide a negative int?
int a = -7, b = 2;
printf("%d\n", a / b);

What does this print?

-3
-4
-3.5
3
Explanation: Since C99, integer division truncates toward zero.
-7 / 2 is mathematically -3.5. Truncating toward zero yields -3 (not -4).
The modulo is defined so that a = (a/b)*b + a%b still holds — here, -7 % 2 = -1.

Question 9 — Cast position matters

Where you put the cast changes the result.
int a = 7, b = 2;
printf("%f\n", (double)(a / b));

What does this print?

3.500000
3.000000
3
Compile error
Explanation: The position of the cast is what matters.
(1) The parenthesized expression a / b is evaluated first (int / int → 7/2 = 3, truncated).
(2) The result 3 is then cast to (double) → 3.0.
(3) Printed with %f3.000000.

To perform real floating-point division, cast before the division: (double)a / b. At least one operand has to be a double at the moment the division happens.

Related lessons

Operators
Arithmetic operators
C arithmetic operators (+, -, *, /, %) with diagrams, plus integer division pitfalls.
Operators
Assignment & increment
Assignment operators and increment (++)/decrement (--) in C.
Operators
Comparison & logical operators
Comparison (==, !=, <, >) and logical (&&, ||, !) operators in C.
← Previous lesson
Lesson 8: Arithmetic Operators
Next lesson →
Lesson 10: Assignment & Increment