🇯🇵 日本語 | 🇺🇸 English
Advertisement

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 mathematically 1.4, but the fractional part is truncated to give 1.
Always remember: integer division 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 becomes double / int, and b is then also promoted to double.
7.0 / 5.0 = 1.4 → 1.400000.
Casting one operand lets you do floating-point division between 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 works only on integer types — not on doubles.
Common uses include even/odd tests (e.g. n % 2 == 0) and digit extraction.
Advertisement

Related lessons

Operators
Arithmetic operators
C arithmetic operators (+, -, *, /, %) with diagrams. 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 7: Arithmetic Operators
Next lesson →
Lesson 9: Assignment & Increment