Skip to content
StudyFame

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).

OperatorNameExample (a = 17, b = 5)Result
+Additiona + b22
-Subtractiona - b12
*Multiplicationa * b85
/Divisiona / b3 (not 3.4!)
%Modulus (remainder)a % b2

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 % 2 is a compile error (use fmod() 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

  1. a / b: both are int, so 17 / 5 = 3 (integer division).
  2. a % b: the remainder of 17 ÷ 5 is 2.
  3. a / 5.0: 5.0 is a double, so the answer is a decimal: 3.4.
  4. -7 / 2: −3.5 is cut toward zero to −3.
  5. -7 % 3: −7 = 3 × (−2) + (−1), so the remainder is −1 (it takes the sign of −7).

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 % b follows a.
  • n % 10 = last digit; n % 2 == 0 = even.

Common Mistakes Beginners Make

MistakeWhat happensFix
float avg = sum / 3; with int sumDecimal part lostsum / 3.0f
x % 2.5Compile error% only for integers
x / 0 or x % 0Runtime crashCheck the divisor is not 0
Writing 2x for 2 × xErrorAlways write 2 * x
Writing x^2 for x squared^ is bitwise XOR, not powerx * x or pow(x, 2)

Arithmetic Operators Practice Questions

  1. Name the five arithmetic operators in C.
  2. What is the output of printf("%d", 25 / 4);?
  3. What is 25 % 4? What is -25 % 4?
  4. Can the modulus operator be used with float values?
  5. How do you get the last digit of a number?
Show answers
  1. +, -, *, /, %.
  2. 6 (integer division).
  3. 25 % 4 = 1. -25 % 4 = −1 (sign follows the first number).
  4. No. Use fmod() from math.h for decimals.
  5. 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)

  1. Output of printf("%d", 10 / 4);

    A) 2.5   B) 2   C) 3   D) 2.0

  2. Output of printf("%d", 10 % 4);

    A) 2   B) 2.5   C) 4   D) 0

  3. Which is an error?

    A) 7 % 2   B) 7.0 / 2   C) 7.5 % 2   D) -7 / 2

  4. Value of -9 % 4:

    A) 1   B) −1   C) 3   D) −3

  5. A number n is even if:

    A) n / 2 == 0   B) n % 2 == 0   C) n % 2 == 1   D) n * 2 == 0

Show answers
  1. B: 2.
  2. A: 2.
  3. C: % cannot be used with a float.
  4. B: −1.
  5. 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 / 53 (integer division)
17 % 52 (remainder)
17 / 5.03.4
-7 / 2−3 (cut toward zero)
-7 % 3−1 (sign of first number)
% with floatError; use fmod()
Last digit / evenn % 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; use pow() 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.