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.
+, -, *, /, and
%.
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.
| Operator | Name | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Remainder | a % b |
The addition operator + adds two or more numeric values.
int a = 10;
int b = 20;
int sum = a + b;
printf("%d", sum);
Output:
30
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.
int price = 100;
int tax = 20;
int total = price + tax;
printf("Total = %d", total);
Output:
Total = 120
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
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.
int total = 1000;
int expense = 250;
int balance = total - expense;
printf("Balance = %d", balance);
Output:
Balance = 750
The multiplication operator * multiplies two numeric values.
int a = 10;
int b = 5;
int result = a * b;
printf("%d", result);
Output:
50
int a = 2;
int b = 3;
int c = 4;
int result = a * b * c;
printf("%d", result);
Output:
24
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.
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
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.
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
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.
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
int number = 20;
printf("%d", number % 2);
Output:
0
A number that gives a remainder of zero when divided by 2 is even.
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.
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.
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
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
#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.
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
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
#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
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
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
+ with ++.* with pointer dereferencing.#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;
}
Practice the following programs using arithmetic operators:
%.Practicing these calculations will make it easier to understand expressions, operator precedence, and later C programming topics.
+ is used for addition.- is used for subtraction.* is used for multiplication./ is used for division.% gives the remainder of integer division.Question: Which operator is used to find the remainder after integer division in C?