Lesson 15 of 60 – Arithmetic Operators in C
25%

Arithmetic Operators in C

Arithmetic operators are used to perform mathematical calculations in C. They allow us to add, subtract, multiply, divide, and find the remainder between numeric values.

Note: The main arithmetic operators in C are +, -, *, /, and %.

1. What are Arithmetic Operators?

Arithmetic operators are symbols used to perform mathematical operations on numeric operands.

int a = 20;
int b = 10;

int result = a + b;

Here, + is an arithmetic operator used to add two values.

2. Arithmetic Operators in C

Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Remainder a % b

3. Addition Operator +

The addition operator + adds two or more numeric values.

int a = 10;
int b = 20;

int sum = a + b;

printf("%d", sum);

Output:

30

4. Addition of Three Numbers

int a = 10;
int b = 20;
int c = 30;

int sum = a + b + c;

printf("%d", sum);

Output:

60

The addition operation is performed from left to right according to the expression's evaluation rules.

5. Addition with Variables

int price = 100;
int tax = 20;

int total = price + tax;

printf("Total = %d", total);

Output:

Total = 120

6. Subtraction Operator -

The subtraction operator - subtracts the right operand from the left operand.

int a = 50;
int b = 20;

int result = a - b;

printf("%d", result);

Output:

30

7. Subtraction of Multiple Values

int a = 100;
int b = 20;
int c = 10;

int result = a - b - c;

printf("%d", result);

Output:

70

The expression is evaluated from left to right for subtraction.

8. Subtraction with Variables

int total = 1000;
int expense = 250;

int balance = total - expense;

printf("Balance = %d", balance);

Output:

Balance = 750

9. Multiplication Operator *

The multiplication operator * multiplies two numeric values.

int a = 10;
int b = 5;

int result = a * b;

printf("%d", result);

Output:

50

10. Multiplication of Three Numbers

int a = 2;
int b = 3;
int c = 4;

int result = a * b * c;

printf("%d", result);

Output:

24

11. Multiplication in a Calculation

int price = 50;
int quantity = 4;

int total = price * quantity;

printf("Total = %d", total);

Output:

Total = 200

Multiplication is commonly used when calculating quantities, prices, areas, and many other values.

12. Division Operator /

The division operator / divides the left operand by the right operand.

int a = 20;
int b = 5;

int result = a / b;

printf("%d", result);

Output:

4

13. Integer Division

When both operands are integers, C performs integer division. The fractional part is discarded.

int a = 7;
int b = 2;

int result = a / b;

printf("%d", result);

Output:

3

The mathematical result is 3.5, but integer division produces 3.

14. Floating-Point Division

To preserve a fractional result, use floating-point operands.

float a = 7.0;
float b = 2.0;

float result = a / b;

printf("%.2f", result);

Output:

3.50

15. Division by Zero

Division by zero must be avoided. A C program should not attempt to divide an integer by zero.

int a = 10;
int b = 0;

/* a / b is invalid */

Always check that the divisor is not zero before performing a division when the value may come from user input or another variable.

16. Remainder Operator %

The remainder operator % gives the remainder left after integer division.

int a = 17;
int b = 5;

int remainder = a % b;

printf("%d", remainder);

Output:

2

17. Remainder with Even Numbers

int number = 20;

printf("%d", number % 2);

Output:

0

A number that gives a remainder of zero when divided by 2 is even.

18. Remainder with Odd Numbers

int number = 15;

printf("%d", number % 2);

Output:

1

For a positive integer, a remainder of 1 after division by 2 indicates an odd number.

19. Using Arithmetic Operators Together

int a = 20;
int b = 10;

int result = a + b * 2;

printf("%d", result);

Output:

40

Multiplication is performed before addition according to operator precedence.

20. Using Parentheses

Parentheses can be used to control the order in which arithmetic operations are performed.

int a = 20;
int b = 10;

int result = (a + b) * 2;

printf("%d", result);

Output:

60

21. Arithmetic Expression

An arithmetic expression is a combination of values, variables, operators, and parentheses that produces a value.

int a = 10;
int b = 5;
int c = 2;

int result = (a + b) * c;

printf("%d", result);

Output:

30

22. Arithmetic Operators with User Input

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    printf("Sum = %d\n", a + b);
    printf("Difference = %d\n", a - b);
    printf("Product = %d\n", a * b);

    return 0;
}

This program accepts two numbers and performs three arithmetic operations.

23. Calculate Average

Arithmetic operators can be used to calculate the average of numbers.

#include <stdio.h>

int main()
{
    int a = 80;
    int b = 70;
    int c = 90;

    float average = (a + b + c) / 3.0;

    printf("Average = %.2f", average);

    return 0;
}

Output:

Average = 80.00

24. Calculate Area of Rectangle

Multiplication can be used to calculate the area of a rectangle.

#include <stdio.h>

int main()
{
    int length = 10;
    int width = 5;

    int area = length * width;

    printf("Area = %d", area);

    return 0;
}

Output:

Area = 50

25. Calculate Simple Total

#include <stdio.h>

int main()
{
    int price = 500;
    int quantity = 3;

    int total = price * quantity;

    printf("Total Amount = %d", total);

    return 0;
}

Output:

Total Amount = 1500

26. Arithmetic Operators and Negative Values

Arithmetic operators can also work with negative numeric values.

int a = -10;
int b = 5;

printf("%d\n", a + b);
printf("%d\n", a - b);
printf("%d\n", a * b);

Output:

-5
-15
-50

27. Arithmetic Assignment Operators

Arithmetic calculations can also be combined with assignment.

int number = 10;

number += 5;
printf("%d\n", number);

number *= 2;
printf("%d\n", number);

number -= 5;
printf("%d\n", number);

Output:

15
30
25

28. Common Arithmetic Mistakes

  • Confusing + with ++.
  • Confusing multiplication * with pointer dereferencing.
  • Forgetting that integer division discards the fractional part.
  • Attempting to divide by zero.
  • Using the remainder operator with floating-point operands.
  • Forgetting parentheses when a different calculation order is required.
  • Using an incorrect format specifier when displaying a result.

29. Complete Arithmetic Program

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter first number: ");
    scanf("%d", &a);

    printf("Enter second number: ");
    scanf("%d", &b);

    printf("Addition = %d\n", a + b);
    printf("Subtraction = %d\n", a - b);
    printf("Multiplication = %d\n", a * b);

    if (b != 0)
    {
        printf("Division = %d\n", a / b);
        printf("Remainder = %d\n", a % b);
    }
    else
    {
        printf("Division and remainder are not possible with zero divisor.\n");
    }

    return 0;
}

30. Practice with Arithmetic Operators

Practice the following programs using arithmetic operators:

  1. Add two numbers.
  2. Subtract two numbers.
  3. Multiply two numbers.
  4. Divide two numbers.
  5. Find the remainder of two numbers.
  6. Calculate the area of a rectangle.
  7. Calculate the average of three numbers.
  8. Calculate the total price using quantity and price.
  9. Find whether a number is even or odd using %.
  10. Create a simple calculator using arithmetic operators.

Practicing these calculations will make it easier to understand expressions, operator precedence, and later C programming topics.

📌 Key Points

  • + is used for addition.
  • - is used for subtraction.
  • * is used for multiplication.
  • / is used for division.
  • % gives the remainder of integer division.
  • Integer division discards the fractional part.
  • Use floating-point operands when a fractional division result is required.
  • Never divide an integer by zero.
  • Parentheses can be used to control the order of calculations.
  • Arithmetic operators can be combined with assignment operators.

🧠 Quick Quiz

Question: Which operator is used to find the remainder after integer division in C?