ARITHMETIC OPERATORS
The symbols of the arithmetic operators are:-
| Operation | Operator | Comment | Value of Sum before | Value of sum after |
| Multiply | * | sum = sum * 2; | 4 | 8 |
| Divide | / | sum = sum / 2; | 4 | 2 |
| Addition | + | sum = sum + 2; | 4 | 6 |
| Subtraction | - | sum = sum -2; | 4 | 2 |
| Increment | ++ | ++sum; | 4 | 5 |
| Decrement | -- | --sum; | 4 | 3 |
| Modulus | % | sum = sum % 3; | 4 | 1 |
The following code fragment adds the variables loop and count together, leaving the result in the variable sum
sum = loop + count;
Note: If the modulus % sign is needed to be displayed as part of a text string, use two, ie %%
#include <stdio.h>
main()
{
int sum = 50;
float modulus;
modulus = sum % 10;
printf("The %% of %d by 10 is %f\n", sum, modulus);
}
Sample Program Output
The % of 50 by 10 is 0.000000
CLASS EXERCISE C5
What does the following change do to the printed output of the
previous program?
printf("The %% of %d by 10 is %.2f\n", sum, modulus);
Increment
The increment operator adds one to the value of
the variable. The following code fragment (part of a program)
adds one to the value of count, so that after the statement is
executed, count has a value of 5.
int count = 4; count++;
Decrement
The decrement operator subtracts one from the value of the
variable. The following code fragment (part of a program)
subtracts one from the value of count, so that after the
statement is executed, count has a value of 3.
int count = 4; count++;
Modulus
The modulus operator assigns the remainder left over after a
division the value of the variable. The following code fragment (part
of a program) uses the modulus operator to calculate the modulus
of 20 % 3. To work this out, divide 20 by 3. Now 3 divides into
20 six times, with a remainder left over of 2. So the value 2 (the
remainder) is assigned to count.
int count; count = 20 % 3;