Arithmetic Operators
Arithmetic operators
Arithmetic operators are special symbols in C that perform specific mathematical operations on one, two, or three operands, and then return a result.
Below are the Arithmetic operators in C with a brief description:
Operator | Description | Example | Result |
---|---|---|---|
+ | Adds two operands. | 5 + 10 | 15 |
- | Subtracts the second operand from the first. | 10 - 5 | 5 |
* | Multiplies the operands. | 5 * 15 | 75 |
/ | Divides the first operand by the second. | 15 / 5 | 3 |
% | Returns the remainder of the division of the first operand by the second. | 7 % 3 | 1 |
Examples of Arithmetic operators:
Example 1:
#include <stdio.h>
int main() {
int x = 5, y = 10, z = 15;
// Addition operator
int sum = x + y; // sum = 15
printf("%d + %d = %d\n", x, y, sum);
// Subtraction operator
int difference = y - x; // difference = 5
printf("%d - %d = %d\n", y, x, difference);
// Multiplication operator
int product = x * z; // product = 75
printf("%d * %d = %d\n", x, z, product);
// Division operator
int quotient = z / x; // quotient = 3
printf("%d / %d = %d\n", z, x, quotient);
// Modulus operator
int remainder = y % x; // remainder = 0
printf("%d %% %d = %d\n", y, x, remainder);
return 0;
}
Output:
Explanation
The program uses the following arithmetic operators:
The addition operator (+) is used to add two operands
(x and y)
together. The result is stored in the variablesum
, which is then printed using theprintf
function.The subtraction operator (-) is used to subtract one operand
(x)
from another(y)
. The result is stored in the variabledifference
, which is then printed using theprintf
function.The multiplication operator (*) is used to multiply two operands
(x and z)
together. The result is stored in the variableproduct
, which is then printed using theprintf
function.The division operator (/) is used to divide one operand
(z)
by another(x)
. The result is stored in the variablequotient
, which is then printed using theprintf
function.The modulus operator (%)
is used to find the remainder when one operand(y)
is divided by another(x)
. The result is stored in the variableremainder
, which is then printed using theprintf
function.
When run, this program will print:
5 + 10 = 15
10 - 5 = 5
5 * 15 = 75
15 / 5 = 3
10 % 5 = 0
/
vs %
Please note that in C, the division operator (/)
will return the quotient of the division and the modulus operator (%)
returns the remainder of the division.