Arithmetic Operators in C
Arithmetic operators do maths: add, subtract, multiply, divide and find the remainder. Most are simple, but / and % have rules that surprise almost every beginner.
In this lesson you will learn all five operators, the integer division rule, how % works with negative numbers, and two very useful tricks: checking even/odd and getting the last digit.
What You Will Learn
- The 5 arithmetic operators.
- Integer division vs decimal division.
- How the modulus operator
%works, including negatives. - Using
%to check even/odd and to get the last digit. - Unary plus and minus.
Arithmetic Operators in C Explained
Exam definition: Arithmetic operators perform mathematical operations. C has five: + (addition), − (subtraction), * (multiplication), / (division) and % (modulus, remainder).
| Operator | Name | Example (a = 17, b = 5) | Result |
|---|---|---|---|
+ | Addition | a + b | 22 |
- | Subtraction | a - b | 12 |
* | Multiplication | a * b | 85 |
/ | Division | a / b | 3 (not 3.4!) |
% | Modulus (remainder) | a % b | 2 |
Rule 1: integer ÷ integer = integer
- If both numbers are integers,
/gives only the whole part. The decimal part is thrown away (not rounded):17 / 5= 3,9 / 10= 0. - If at least one number is a decimal, you get a decimal answer:
17 / 5.0= 3.4. - With negatives, the result is cut toward zero:
-7 / 2= −3.
Rule 2: % gives the remainder, only for integers
17 % 5= 2, because 17 = 5 × 3 + 2.%works only with integers.5.5 % 2is a compile error (usefmod()for decimals).- The sign of the result follows the first number:
-7 % 3= −1,7 % -3= 1.
Memory trick: Division in C is like sharing sweets: 17 sweets among 5 children → each gets 3 (that's /), and 2 are left over (that's %). No child gets a broken sweet, so no decimals.
Syntax of Arithmetic Operators
result = operand1 + operand2;
result = operand1 % operand2; /* both must be integers */
result = -operand; /* unary minus */
Example 1: All Arithmetic Operators
#include <stdio.h>
int main(void)
{
int a = 17, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("a / 5.0 = %.1f\n", a / 5.0);
printf("-7 / 2 = %d, -7 %% 3 = %d\n", -7 / 2, -7 % 3);
return 0;
}
Output
a + b = 22 a - b = 12 a * b = 85 a / b = 3 a % b = 2 a / 5.0 = 3.4 -7 / 2 = -3, -7 % 3 = -1
Code Explanation
a / b: both areint, so 17 / 5 = 3 (integer division).a % b: the remainder of 17 ÷ 5 is 2.a / 5.0: 5.0 is adouble, so the answer is a decimal: 3.4.-7 / 2: −3.5 is cut toward zero to −3.-7 % 3: −7 = 3 × (−2) + (−1), so the remainder is −1 (it takes the sign of −7).
How Integer Division and Modulus Are Related
For integers, C always keeps this rule true:
a == (a / b) * b + (a % b)
Check with 17 and 5: (17 / 5) × 5 + (17 % 5) = 3 × 5 + 2 = 17. ✔
Check with −7 and 3: (−7 / 3) × 3 + (−7 % 3) = (−2) × 3 + (−1) = −7. ✔
Example 2: Even/Odd Check and Last Digit with %
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Last digit: %d\n", n % 10);
printf("Without last digit: %d\n", n / 10);
printf("%d is %s\n", n, (n % 2 == 0) ? "even" : "odd");
return 0;
}
Output
Enter a number: 4729 Last digit: 9 Without last digit: 472 4729 is odd
n % 10 gives the last digit; n / 10 removes it. n % 2 is 0 for even numbers and 1 for odd numbers. These tricks are used in many programs: sum of digits, reverse a number, palindrome and Armstrong number.
Important Points to Remember
- 5 arithmetic operators: + − * / %.
- int / int = int: the decimal part is cut, not rounded.
%gives the remainder and works only with integers.- The sign of
a % bfollows a. n % 10= last digit;n % 2 == 0= even.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
float avg = sum / 3; with int sum | Decimal part lost | sum / 3.0f |
x % 2.5 | Compile error | % only for integers |
x / 0 or x % 0 | Runtime crash | Check the divisor is not 0 |
Writing 2x for 2 × x | Error | Always write 2 * x |
Writing x^2 for x squared | ^ is bitwise XOR, not power | x * x or pow(x, 2) |
Arithmetic Operators Practice Questions
- Name the five arithmetic operators in C.
- What is the output of
printf("%d", 25 / 4);? - What is
25 % 4? What is-25 % 4? - Can the modulus operator be used with float values?
- How do you get the last digit of a number?
Show answers
+,-,*,/,%.- 6 (integer division).
25 % 4= 1.-25 % 4= −1 (sign follows the first number).- No. Use
fmod()frommath.hfor decimals. n % 10
Coding Exercise
Read a 3-digit number and print the sum of its digits using only / and % (no loops). Example: 472 → 4 + 7 + 2 = 13.
Show solution
#include <stdio.h>
int main(void)
{
int n, first, middle, last;
printf("Enter a 3-digit number: ");
scanf("%d", &n);
first = n / 100;
middle = (n / 10) % 10;
last = n % 10;
printf("Sum of digits = %d\n", first + middle + last);
return 0;
}
Enter a 3-digit number: 472 Sum of digits = 13
Quick Quiz: Arithmetic Operators in C (MCQ)
-
Output of
printf("%d", 10 / 4);A) 2.5 B) 2 C) 3 D) 2.0
-
Output of
printf("%d", 10 % 4);A) 2 B) 2.5 C) 4 D) 0
-
Which is an error?
A)
7 % 2B)7.0 / 2C)7.5 % 2D)-7 / 2 -
Value of
-9 % 4:A) 1 B) −1 C) 3 D) −3
-
A number
nis even if:A)
n / 2 == 0B)n % 2 == 0C)n % 2 == 1D)n * 2 == 0
Show answers
- B: 2.
- A: 2.
- C:
%cannot be used with a float. - B: −1.
- B:
n % 2 == 0
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Operators | + - * / % |
|---|---|
| 17 / 5 | 3 (integer division) |
| 17 % 5 | 2 (remainder) |
| 17 / 5.0 | 3.4 |
| -7 / 2 | −3 (cut toward zero) |
| -7 % 3 | −1 (sign of first number) |
| % with float | Error; use fmod() |
| Last digit / even | n % 10 / n % 2 == 0 |
Frequently asked questions
- Why does 7 / 2 give 3 in C?
- Both are integers, so C does integer division and drops the decimal part.
- Can % be used with floating-point numbers?
- No. Use
fmod()from math.h. - What is the sign of the result of a % b?
- The same as a (the first number).
- Is ^ the power operator in C?
- No.
^is bitwise XOR; usepow()for power.
Remember
- Sharing sweets:
/= each child's share,%= left over. - a == (a / b) * b + a % b.
Next Topic
Next lesson: Relational Operators in C. You will learn to compare values, and the famous = vs == mistake.